我可以创建和显示一个自定义警报对话框很好,但即使这样,我有android:layout_width/height=“fill_parent”在对话xml中,它只和内容一样大。
我想要的是填充整个屏幕的对话框,除了20像素的填充。 然后,作为对话框一部分的图像将自动使用fill_parent拉伸到完整的对话框大小。
我可以创建和显示一个自定义警报对话框很好,但即使这样,我有android:layout_width/height=“fill_parent”在对话xml中,它只和内容一样大。
我想要的是填充整个屏幕的对话框,除了20像素的填充。 然后,作为对话框一部分的图像将自动使用fill_parent拉伸到完整的对话框大小。
当前回答
我的答案是基于koma的,但它不需要覆盖onStart,但只需要onCreateView,当你创建新的片段时,默认情况下几乎总是被覆盖。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.your_fragment_layout, container);
Rect displayRectangle = new Rect();
Window window = getDialog().getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
v.setMinimumWidth((int)(displayRectangle.width() * 0.9f));
v.setMinimumHeight((int)(displayRectangle.height() * 0.9f));
return v;
}
我在Android 5.0.1上进行了测试。
其他回答
***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
}
试试这个:
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
public static WindowManager.LayoutParams setDialogLayoutParams(Activity activity, Dialog dialog)
{
try
{
Display display = activity.getWindowManager().getDefaultDisplay();
Point screenSize = new Point();
display.getSize(screenSize);
int width = screenSize.x;
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.copyFrom(dialog.getWindow().getAttributes());
layoutParams.width = (int) (width - (width * 0.07) );
layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
return layoutParams;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
您需要使用样式@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>
我发现了一个非常简单易行的解决办法
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)
}