我可以创建和显示一个自定义警报对话框很好,但即使这样,我有android:layout_width/height=“fill_parent”在对话xml中,它只和内容一样大。
我想要的是填充整个屏幕的对话框,除了20像素的填充。 然后,作为对话框一部分的图像将自动使用fill_parent拉伸到完整的对话框大小。
我可以创建和显示一个自定义警报对话框很好,但即使这样,我有android:layout_width/height=“fill_parent”在对话xml中,它只和内容一样大。
我想要的是填充整个屏幕的对话框,除了20像素的填充。 然后,作为对话框一部分的图像将自动使用fill_parent拉伸到完整的对话框大小。
当前回答
我发现了一个非常简单易行的解决办法
fun showDialog(){
val dialog = Dialog(this@DialogActivity)
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.setCancelable(false)
dialog.setContentView(R.layout.custom_dialog)
val txtTitle = dialog.findViewById<TextView>(R.id.txtTitle)
val btn = dialog.findViewById<Button>(R.id.button)
btn.setOnClickListener {
Toast.makeText(this,"test",Toast.LENGTH_SHORT).show()
}
txtTitle.setText("ali")
dialog.show()
val window = dialog.window
window?.setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.WRAP_CONTENT)
}
其他回答
在对话框窗口上指定FILL_PARENT,就像其他人建议的那样,对我来说并不管用(在Android 4.0.4上),因为它只是拉伸了黑色对话框背景来填充整个屏幕。
正常工作的方法是使用最小显示值,但在代码中指定它,以便对话框占用屏幕的90%。
So:
Activity activity = ...;
AlertDialog dialog = ...;
// retrieve display dimensions
Rect displayRectangle = new Rect();
Window window = activity.getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
// inflate and adjust layout
LayoutInflater inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.your_dialog_layout, null);
layout.setMinimumWidth((int)(displayRectangle.width() * 0.9f));
layout.setMinimumHeight((int)(displayRectangle.height() * 0.9f));
dialog.setView(layout);
一般来说,在大多数情况下,只调整宽度就足够了。
这里其他的答案都说得通,但它不符合费边的要求。这是我的一个解决办法。这可能不是完美的解决方案,但对我来说很管用。它显示了一个全屏的对话框,但你可以在顶部、底部、左边或右边指定一个填充。
首先把它放在res/values/styles.xml中:
<style name="CustomDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@color/Black0Percent</item>
<item name="android:paddingTop">20dp</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowIsFloating">false</item>
</style>
正如你所看到的,我有android:paddingTop= 20dp基本上是你需要的。android:windowBackground = @color/Black0Percent只是在color.xml中声明的颜色代码
res /价值/ color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="Black0Percent">#00000000</color>
</resources>
该颜色代码只是作为一个虚拟,以取代对话框的默认窗口背景与0%透明度的颜色。
接下来构建自定义对话框布局res/layout/dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialoglayout"
android:layout_width="match_parent"
android:background="@drawable/DesiredImageBackground"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/edittext1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textSize="18dp" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dummy Button"
android:textSize="18dp" />
</LinearLayout>
最后这是我们的对话框,它设置了使用dialog.xml的自定义视图:
Dialog customDialog;
LayoutInflater inflater = (LayoutInflater) getLayoutInflater();
View customView = inflater.inflate(R.layout.dialog, null);
// Build the dialog
customDialog = new Dialog(this, R.style.CustomDialog);
customDialog.setContentView(customView);
customDialog.show();
结论:我试图在名为CustomDialog的styles.xml中覆盖对话框的主题。它覆盖了对话框窗口布局,让我有机会设置填充和改变背景的不透明度。这可能不是完美的解决方案,但我希望它能帮助你…:)
final AlertDialog alertDialog;
LayoutInflater li = LayoutInflater.from(mActivity);
final View promptsView = li.inflate(R.layout.layout_dialog_select_time, null);
RecyclerView recyclerViewTime;
RippleButton buttonDone;
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mActivity);
alertDialogBuilder.setView(promptsView);
// create alert dialog
alertDialog = alertDialogBuilder.create();
/**
* setting up window design
*/
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.show();
DisplayMetrics metrics = new DisplayMetrics(); //get metrics of screen
mActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = (int) (metrics.heightPixels * 0.9); //set height to 90% of total
int width = (int) (metrics.widthPixels * 0.9); //set width to 90% of total
alertDialog.getWindow().setLayout(width, height); //set layout
recyclerViewTime = promptsView.findViewById(R.id.recyclerViewTime);
DialogSelectTimeAdapter dialogSelectTimeAdapter = new DialogSelectTimeAdapter(this);
RecyclerView.LayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerViewTime.setLayoutManager(linearLayoutManager);
recyclerViewTime.setAdapter(dialogSelectTimeAdapter);
buttonDone = promptsView.findViewById(R.id.buttonDone);
buttonDone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
alertDialog.dismiss();
}
});
dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.WRAP_CONTENT);
下面是我的自定义对话框宽度的变体:
DisplayMetrics displaymetrics = new DisplayMetrics();
mActivity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = (int) (displaymetrics.widthPixels * (ThemeHelper.isPortrait(mContext) ? 0.95 : 0.65));
WindowManager.LayoutParams params = getWindow().getAttributes();
params.width = width;
getWindow().setAttributes(params);
因此,根据设备方向(ThemeHelper.isPortrait(mContext))对话框的宽度将是95%(纵向模式)或65%(横向模式)。这比作者要求的要多一点,但对某些人来说可能有用。
你需要创建一个从Dialog扩展而来的类,并把这段代码放到你的onCreate(Bundle savedInstanceState)方法中。
对于对话框的高度,代码应该类似于此。