繁体   English   中英

如何让Android TableLayout以横向模式填充父级?

[英]How can I get an Android TableLayout to fill the parent in landscape mode?

我在我的应用程序中使用TableLayout。 它包含四个TableRows,每个TableRows包含四个ImageView。 我想要的行为是缩放布局以适应最短的尺寸。

它在纵向模式下工作正常但在横向模式下失败。

文档看起来这是因为TableRow layout_height始终设置为WRAP_CONTENT。 虽然我可以设置各个视图的尺寸,但如果我将View layout_height设置为FILL_PARENT,则TableLayout根本不会渲染。

我觉得我有一些简单的东西。 有没有办法让TableLay与TableRows一起缩放以适应横向模式的高度?

XML布局:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/table"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:shrinkColumns="*">
</TableLayout>

Java的:

public class Example extends Activity {

private TableLayout mTable;
private int[] mDrawableIds = {  R.drawable.draw01, R.drawable.draw02, R.drawable.draw03, R.drawable.draw04, R.drawable.draw05, R.drawable.draw06,   R.drawable.draw07, R.drawable.draw08, R.drawable.draw09, R.drawable.draw10, R.drawable.draw11, R.drawable.draw12, R.drawable.draw13, R.drawable.draw14, R.drawable.draw15, R.drawable.draw16 };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mTable = (TableLayout)findViewById(R.id.table);
    for (int j=0; j<4; j++) {
        TableRow tr = new TableRow(this);
        tr.setLayoutParams(new ViewGroup.LayoutParams(  LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
           for (int i=0; i<4; i++) {
               ImageView iv = new ImageView(this);
               iv.setImageResource(mDrawableIds[j*4+i]);
               iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
               iv.setAdjustViewBounds(true);
               tr.addView(iv);
           }
        mTable.addView(tr);
    }
}
}

将XML布局更改为:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/table"
        android:layout_width="fill_parent"
        android:layout_height="160dp"
        android:shrinkColumns="*">
</TableLayout>

这应该使它占据整个屏幕的高度,无论方向如何。 请参见密度无关像素

我确实弄清楚如何做到这一点。 基本上,没有按照我想要的方式工作的部分是TableRows。 当我改为横向时,前两个TableRows根本没有调整大小,第三个消耗剩下的空间。 第四行根本没有显示。

我发现我需要将TableRows的layout_weight设置为相等的值。 但是,在运行时没有方法可以做到这一点,所以我构建了以下布局文件,我称之为row.xml:

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/trow"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_weight="1">
</TableRow>

然后我使用LayoutInflater在运行时使用我想要的属性构建行:

    mTable = (TableLayout)findViewById(R.id.table);
    LayoutInflater inflater = getLayoutInflater();

    for (int j=0; j<4; j++) {
        View row = inflater.inflate(R.layout.row, mTable,false);
        TableRow tr = (TableRow)row.findViewById(R.id.trow);
           for (int i=0; i<4; i++) {
               View image = inflater.inflate(R.layout.image, tr, false);
               ImageButton ib = (ImageButton)image.findViewById(R.id.image);
               ib.setAdjustViewBounds(true);
               ib.setImageResource(mCardIds[j*4+i]);
               tr.addView(ib);
           }
        mTable.addView(tr);
    }

现在TableRows正在轮换时正确调整大小。 我将ImageViews更改为ImageButtons,因为我决定添加一些onClick行为,我通过单独的xml文件构建这些按钮,但我不认为这些细节与调整大小问题有关。

我在这里尝试了接受的解决方案,但它没有用。 有效的方法如下:

TableRow tr = new TableRow(this);
TableLayout.LayoutParams pRowTop = new TableLayout.LayoutParams(
                TableLayout.LayoutParams.MATCH_PARENT,
                TableLayout.LayoutParams.WRAP_CONTENT);
pRowTop.weight = 1;
mTable.addView(tr, pRowTop);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM