gpt4 book ai didi

android - 如何在对话框中设置边距

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:31:58 25 4
gpt4 key购买 nike

我在我的 Andorid 应用程序中使用了 Dialog 来显示广告。但是我必须显示这个 Dialog 从按钮顶部大约 50dp 所以我认为我们应该设置 Dialog Gravity buttom 并将其按钮边距设置为 50dp。但是我无法在 Dialog 中使用边距。所以请建议我如何解决这个问题。

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_element"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/dialogback"
android:orientation="vertical" >

<WebView
android:id="@+id/webView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

Java:

final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
LayoutInflater inflator = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflator.inflate(R.layout.ad, null, false);
dialog.setContentView(view);
dialog.getWindow().setGravity(Gravity.BOTTOM);
dialog.setCancelable(true);
WebView webView = (WebView) dialog.findViewById(R.id.webView);
webView.loadUrl("");
webView.setWebViewClient(new MyWebViewClient());
webView.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
dialog.show();

最佳答案

我做了一个类似的笑脸对话。我扩展对话框

public class SmileCustomDialog extends Dialog {
Context mcontext;
GridView mGridview;

public GridView getGridview() {
return mGridview;
}

public SmileCustomDialog(final Context context) {
super(context, R.style.SlideFromBottomDialog);
this.mcontext = context;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.emocategorydialog, null);
mGridview = (GridView) v.findViewById(R.id.emogrid);
mGridview.setSelector(new ColorDrawable(Color.TRANSPARENT));

ImageAdapter mAdapter = new ImageAdapter(mcontext);
mGridview.setAdapter(mAdapter);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.setContentView(v);

WindowManager.LayoutParams params = this.getWindow().getAttributes();
this.setCanceledOnTouchOutside(true);
params.y = -100;
this.getWindow().setAttributes(params);

}

}

但本质是

WindowManager.LayoutParams params = yourDialog.getWindow().getAttributes(); // change this to your dialog.

params.y = -100; // Here is the param to set your dialog position. Same with params.x
yourDialog.getWindow().setAttributes(params);

只需在显示对话框之前添加即可。

关于android - 如何在对话框中设置边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13172276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com