0

如何在 Android(基于表格布局)中创建一个宽度与屏幕宽度成比例的对话框(例如 60%)?下面的代码占据了屏幕的所有宽度。

    <?xml version="1.0" encoding="utf-8"?>
    <TableLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        <TableRow
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
            <ImageView
                    android:id="@+id/image"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp"/>
            <TextView
                    android:id="@+id/text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:textColor="#FFF"
                    android:layout_margin="5dp"/>
        </TableRow>
        <TableRow
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
            <Button
                    android:id="@+id/dialogButtonOK"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="Ok"
                    android:gravity="center"
                    android:layout_below="@+id/text"
                    android:layout_span="2"/>
        </TableRow>
    </TableLayout>
4

1 回答 1

0

as far as i know it isn't possible but maybe this might work; in your activity try to access your xml layout- TableLayout tl=(TableLayout)findViewById(...) and try to get your devices screen width

Display display = getWindowManager().getDefaultDisplay(); 
int width=display.getWidth();

and then set some value based on that width via

rl.setLayoutParams(new LayoutParams(width*0.7,LayoutParams.WRAP_CONTENT(or something else) ));
于 2013-04-23T20:05:51.863 回答