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

问题描述:

我在我的应用程序中使用TableLayout。它包含四个TableRows,每个包含四个ImageViews。我想要的行为是缩放布局以适应最短的维度。如何获取Android TableLayout以横向模式填充父级?

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

documentation看起来这是因为TableRow layout_height始终设置为WRAP_CONTENT。虽然我可以设置各个视图的维度,但如果将View layout_height设置为FILL_PARENT,则TableLayout将不会呈现。

我觉得有一些简单的我很想念。有没有办法让TableLayout与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); 
    } 
} 
} 
+0

是否GridView不适用于您的用例? GridView教程在这里:http://developer.android.com/guide/tutorials/views/hello-gridview.html – 2009-12-29 03:30:41

+0

GridView似乎想滚动,而不是调整ImageViews到横向布局的大小。也许如果我把图像资源缩小的话可能会奏效。我希望避免调整图像大小。 – 2009-12-29 20:56:45

我弄清楚如何做到这一点。基本上,不按我想要的方式工作的部分是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文件构建这些按钮,但我不认为这些细节与调整大小问题有关。

+0

为表格标题行中的每一列添加'android:layout_weight =“1”'解决了我的问题,很好 – knownUnknown 2017-01-30 12:55:38

改变你的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> 

这应该使其占据整个屏幕的高度与取向无关。见density independent pixels

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

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