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

发布于 2024-08-15 20:32:37 字数 1943 浏览 6 评论 0原文

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

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

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

我觉得我缺少一些简单的东西。有没有办法让带有 TableRows 的 TableLayout 进行缩放以适应横向模式下的高度?

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);
    }
}
}

I am using a TableLayout in my application. It contains four TableRows each containing four ImageViews. The behavior I want is to scale the layout to fit the shortest dimension.

It works fine in portrait mode but fails miserably in landscape mode.

From the documentation it looks like this is because the TableRow layout_height is always set to WRAP_CONTENT. While I can set the dimensions of the individual Views, the TableLayout won't render at all if I set the View layout_height to FILL_PARENT.

I feel like there is something simple I am missing. Is there a way to get the TableLayout with TableRows to scale to fit the height in landscape mode?

XML Layout:

<?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);
    }
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

相思碎 2024-08-22 20:32:37

将 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>

这应该使其占据整个屏幕的高度,无论方向如何。请参阅密度独立像素

Change your XML layout to:

<?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>

That should make it take up whole screen's height regardless of orientation. See density independent pixels

凹づ凸ル 2024-08-22 20:32:37

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

我发现我需要将 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);
    }

现在 TableRow 正在正确调整大小关于旋转。我将 ImageViews 更改为 ImageButtons,因为我决定添加一些 onClick 行为,并且我通过单独的 xml 文件构建这些按钮,但我认为这些细节与调整大小问题无关。

I did figure out how to do this. Basically, the part that wasn't working the way I wanted was the TableRows. When I changed to landscape, the first two TableRows weren't resizing at all and the third was consuming the remainder of the space. The fourth row wasn't displaying at all.

I figured out that I needed to set the layout_weight of the TableRows to equal values. But, there is no method to do that at runtime, so I built the following layout file, which I called 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>

Then I used a LayoutInflater to build the row at runtime with the properties I wanted:

    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);
    }

Now the TableRows are resizing properly on rotation. I changed the ImageViews to ImageButtons because I decided to add some onClick behavior and I am building those buttons via a separate xml file, but I don't think those details are relevant to the resizing issue.

-小熊_ 2024-08-22 20:32:37

我在这里尝试了公认的解决方案,但它不起作用。工作原理如下:

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);

I tried the accepted solution here but it did not work. What worked is as below:

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文