【问题标题】:Preventing an AlertDialog filling the screen防止 AlertDialog 填满屏幕
【发布时间】:2011-07-22 20:07:23
【问题描述】:

我正在打开一个警报对话框,所有数据都正确显示,但它正在填满屏幕

我打开的对话框是一个简单的布局,然后在其中添加了一个新的布局项目 3 次。问题是对话框几乎全屏打开,即使布局只占一半屏幕

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final View layout = inflater.inflate(R.layout.layout, (ViewGroup) findViewById(R.id.layout));
            LinearLayout ll = (LinearLayout) layout.findViewById(R.id.layout_lookup);

            for (int i = 0; i < 3; i++) {
                View layoutItem = (View) inflater.inflate(R.layout.layout_item, null);
                TextView Name = (TextView) layoutItem .findViewById(R.id.name);
                TextView Value1 = (TextView) layoutItem .findViewById(R.id.value1);
                TextView Value2 = (TextView) layoutItem .findViewById(R.id.value2);

                ll.addView(layoutItem);

                // code setting the textviews
            }

            //iValue = bundle.getInt("iValue");

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setView(layout);

编辑

查找.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="wrap_content"
    android:padding="10dip"
    android:background="@drawable/border_main"
    android:id="@+id/lookup">
</LinearLayout>

lookup_item.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="wrap_content"
        android:padding="10dip"
        android:background="@drawable/border_small"
        android:id="@+id/lookup_item">
        <LinearLayout 
            android:layout_width="match_parent" 
            android:layout_height="wrap_content">
            <TextView 
                android:text=" -- " 
                android:id="@+id/distance"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:textStyle="bold">
            </TextView>
        </LinearLayout>

        <LinearLayout 
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TableLayout 
                android:layout_height="wrap_content" 
                android:layout_width="match_parent" 

            android:id="@+id/tableLayout"
            android:gravity="center_vertical">
            <TableRow 
                android:id="@+id/tableRow" 
                android:layout_width="match_parent" 
                android:layout_height="wrap_content">
                <textView items>
            </TableRow>
        </TableLayout>
    </LinearLayout>
</LinearLayout>

【问题讨论】:

  • this question 建议使用相对布局可能有效。
  • 你能把你的xml布局贴在这里吗...R.id.layout_lookup。

标签: android xml android-alertdialog


【解决方案1】:

在您的扩展 xml 中使用 wrap_content 进行布局

【讨论】:

  • 两个 xml 文件的高度都设置为 wrap_content。是这个意思吗?
  • 你能截个图贴在这里吗?
  • ` getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);` 或尝试移除重力并尝试 wrap_content 布局,方向可能会混淆宽度和高度。我的想法不多了。
猜你喜欢
  • 2012-11-02
  • 2015-09-20
  • 1970-01-01
  • 1970-01-01
  • 2011-02-10
  • 2020-07-20
  • 2018-01-07
  • 2020-01-19
相关资源
最近更新 更多