Dol*_*rma 46 android android-layout android-dialog
在我的应用程序中,我创建的自定义对话框没有完整的高度,我无法更改和自定义.例如看到这个屏幕截图:
我的代码:
final Dialog contacts_dialog = new Dialog(ActivityGroup.this, R.style.theme_sms_receive_dialog);
contacts_dialog.setContentView(R.layout.dialog_schedule_date_time);
contacts_dialog.setCancelable(true);
contacts_dialog.setCanceledOnTouchOutside(true);
contacts_dialog.show();
Run Code Online (Sandbox Code Playgroud)
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layoutRoot"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@null"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape_header_dialog_background"
android:orientation="horizontal"
android:padding="4dp" >
<TextView
android:id="@+id/TextView21"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginRight="5dp"
android:layout_weight="2"
android:gravity="center_vertical|right"
android:text="@string/choose_schedule_time_date"
android:textColor="#ffffff"
android:textSize="14sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/ImageView03"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="0dp"
android:background="@drawable/icon_scudule_time" />
</LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
样式:
<style name="theme_sms_receive_dialog" parent="android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:background">@android:color/transparent</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="numberPickerStyle">@style/NPWidget.Holo.Light.NumberPicker</item>
</style>
Run Code Online (Sandbox Code Playgroud)
Abh*_*ngh 85
您需要获取当前窗口并将其设置为LayoutParams如下所示:
Dialog d=new Dialog(mContext);
.........
.........
myDialog.show();
Window window = myDialog.getWindow();
window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
Run Code Online (Sandbox Code Playgroud)
替代方案(如果以上解决方案不起作用)
如果上面的代码不能正常工作,您可以使用解决方法
styles.xml
<style name="mydialog"></style>
Run Code Online (Sandbox Code Playgroud)
Java的
Dialog d=new Dialog(mContext,R.style.mydialog);
.........
.........
myDialog.show();
Window window = myDialog.getWindow();
window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
Run Code Online (Sandbox Code Playgroud)
Psy*_*her 72
有两种方法可以完成,第一种在style.xml中,第二种在代码中:
Run Code Online (Sandbox Code Playgroud)<style name="Theme_Dialog" parent="android:Theme.Holo.Dialog"> <item name="android:windowMinWidthMajor">90%</item> <item name="android:windowMinWidthMinor">90%</item> </style>
Run Code Online (Sandbox Code Playgroud)final Dialog contacts_dialog = new Dialog(ActivityGroup.this, R.style.theme_sms_receive_dialog); contacts_dialog.setContentView(R.layout.dialog_schedule_date_time);
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
contacts_dialog.setCancelable(true);
contacts_dialog.setCanceledOnTouchOutside(true);
contacts_dialog.show();
Run Code Online (Sandbox Code Playgroud)
小智 10
对于全角度对话框,您可以为对话框创建自定义样式.下面给出了全宽度对话框的代码:
<style name="Custom_Dialog" parent="ThemeOverlay.AppCompat.Light" >
<item name="windowMinWidthMajor">100%</item>
<item name="windowMinWidthMinor">65%</item>
</style>
Run Code Online (Sandbox Code Playgroud)
要将此样式指定给对话框的构造函数,请在以下onCreate()方法之后将其添加到方法setContentView():
getWindow()
.setLayout(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
Run Code Online (Sandbox Code Playgroud)
如果有人想知道如何使用来显示对话框DialogFragment,则可以覆盖onCreateDialog以返回一个对话框,该对话框的自定义样式的windowMinWidthMajor次要设置为90%
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new Dialog(getActivity(), R.style.WideDialog);
}
Run Code Online (Sandbox Code Playgroud)
样式:
<style name="WideDialog" parent="Base.Theme.AppCompat.Dialog">
<item name="android:windowMinWidthMajor">90%</item>
<item name="android:windowMinWidthMinor">90%</item>
</style>
Run Code Online (Sandbox Code Playgroud)
您不需要为此添加任何样式,只需尝试以下一种
Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.your_layout_here);
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
dialog.show();
Run Code Online (Sandbox Code Playgroud)
注意:使用empty style会浪费处理器内部的内存时间。而是直接使用dailog.getWindow().setLayout(width,height)。
小智 5
对于 kotlin 用户
val layoutParams = dialog.window!!.attributes
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT
dialog.window!!.attributes = layoutParams
Run Code Online (Sandbox Code Playgroud)
或者
val width = resources.displayMetrics.widthPixels
val height = dialog.window!!.attributes.height
dailog.window!!.setLayout(width, height)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
62245 次 |
| 最近记录: |