我可以创建和显示一个自定义警报对话框很好,但即使这样,我有android:layout_width/height=“fill_parent”在对话xml中,它只和内容一样大。
我想要的是填充整个屏幕的对话框,除了20像素的填充。 然后,作为对话框一部分的图像将自动使用fill_parent拉伸到完整的对话框大小。
我可以创建和显示一个自定义警报对话框很好,但即使这样,我有android:layout_width/height=“fill_parent”在对话xml中,它只和内容一样大。
我想要的是填充整个屏幕的对话框,除了20像素的填充。 然后,作为对话框一部分的图像将自动使用fill_parent拉伸到完整的对话框大小。
当前回答
***In Kotlin You can Code like This : -***
fun customDialog(activity: Activity?, layout: Int): Dialog {
val dialog = Dialog(activity!!)
try {
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.setCancelable(false)
dialog.setContentView(layout)
dialog.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
dialog.window!!.setLayout(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
dialog.show()
} catch (e: Exception) {
}
return dialog
}
其他回答
下面是我的自定义对话框宽度的变体:
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)方法中。
对于对话框的高度,代码应该类似于此。
到目前为止我能想到的最简单的方法
如果你的对话框是由垂直线性布局组成的,只需添加一个“高度填充”虚拟视图,它将占据屏幕的整个高度。
例如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editSearch" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"/>
<!-- this is a dummy view that will make sure the dialog is highest -->
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
注意LinearLayout属性中的android:weightSum="1"和dummy View属性中的android:layout_weight="1"
你必须在显示之前设置对话框的高度和宽度(dialog.show())
所以,像这样做:
dialog.getWindow().setLayout(width, height);
//then
dialog.show()
您需要使用样式@style.xml,例如CustomDialog来显示可自定义的对话框。
<style name="CustomDialog" parent="@android:style/Theme.DeviceDefault.Light.Dialog">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@color/colorWhite</item>
<item name="android:editTextColor">@color/colorBlack</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
</style>
在activity。java中使用这种样式
Dialog dialog = new Dialog(Activity.this, R.style.CustomDialog);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog);
你的custom_dialog.xml应该在你的布局目录中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="20dp"
android:id="@+id/tittle_text_view"
android:textColor="@color/colorBlack"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp">
<EditText
android:id="@+id/edit_text_first"
android:layout_width="50dp"
android:layout_height="match_parent"
android:hint="0"
android:inputType="number" />
<TextView
android:id="@+id/text_view_first"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:gravity="center"/>
<EditText
android:id="@+id/edit_text_second"
android:layout_width="50dp"
android:layout_height="match_parent"
android:hint="0"
android:layout_marginLeft="5dp"
android:inputType="number" />
<TextView
android:id="@+id/text_view_second"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
根据Android平台开发人员Dianne Hackborn在这个讨论组的帖子,对话框将窗口的顶层布局宽度和高度设置为WRAP_CONTENT。要使对话框更大,可以将这些参数设置为MATCH_PARENT。
演示代码:
AlertDialog.Builder adb = new AlertDialog.Builder(this);
Dialog d = adb.setView(new View(this)).create();
// (That new View is just there to have something inside the dialog that can grow big enough to cover the whole screen.)
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(d.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
d.show();
d.getWindow().setAttributes(lp);
注意,属性是在对话框显示之后设置的。系统对设置时间很挑剔。(我猜布局引擎必须在对话框第一次显示时设置它们。)
最好通过扩展Theme来实现这一点。对话,那么您就不必玩什么时候调用setAttributes的猜谜游戏了。(尽管让对话框自动采用适当的明暗主题或蜂巢Holo主题需要更多的工作。这可以根据http://developer.android.com/guide/topics/ui/themes.html#SelectATheme来做)