我有自定义对话框的这种布局,这适用于普通和小型设备,但是对于大型设备,它的宽度不适合宽度(填充父级),但我可以手动给它一些dp值来实现我想要的尺寸。我想知道为什么默认情况下不填写父工作。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#f70000"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/textViewInvoice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textAppearance="?android:attr/textAppearanceSmall" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/buttonPrint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/buttonPrint" />
<Button
android:id="@+id/buttonCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/buttonCancel" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
答案 0 :(得分:3)
试试这个......
Dialog dialog = new Dialog(YourActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
Window window = dialog.getWindow();
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout lay = new RelativeLayout(YourActivity.this);
View dialogView = inflater.inflate(R.layout.your_dialog_layout,
lay);
dialog.setContentView(dialogView);
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
答案 1 :(得分:2)
使用它。它可能会帮助你
WindowManager.LayoutParams lp1 = new WindowManager.LayoutParams();
lp1.width = WindowManager.LayoutParams.FILL_PARENT;// change this option to your convienience
lp1.height = WindowManager.LayoutParams.WRAP_CONTENT;
Your dailog.getWindow().setAttributes(lp1);
答案 2 :(得分:0)
原因是可能因为包含View的Window。默认情况下它没有占用整个宽度,因为Dialogs不能正常执行此操作。您必须明确设置窗口以占用全宽;这将影响绘制视图之前的计算。哪个应该在show()之前调用。
请记住,当您调用show时,窗口中内容的布局将开始被绘制。