android - TableRow 中以编程方式添加的元素的权重不起作用

标签 android android-layout android-tablelayout

所以,我在一个 xml 文件中有一个 TableLayout 和 4 个 TableRow。我还有一个定义 TextView 的 xml 文件,它将插入行中(每行 3 个 TextView)。 我希望那些 TextView 具有相同的宽度,并且当所有内容都在同一个 xml 文件中时效果很好(开始时我的 TextView 是“硬编码” ) 但现在我以编程方式添加它们,它们的宽度不同。

这是第一个 XML 文件

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/homeTable"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/lmbg"
    android:clickable="true"
    android:padding="20dp" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:minHeight="200dp" >
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:minHeight="200dp" >
    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:minHeight="200dp" >
    </TableRow>

    <TableRow
        android:id="@+id/tableRow4"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:minHeight="200dp" >
    </TableRow>

</TableLayout>

这是我的 TextView 项目

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:layout_weight="1"
    android:clickable="true"
    android:gravity="center"
    android:onClick="goToList"
    android:textSize="14sp" />

最后这就是我以编程方式添加它们的方式

for(int i = 0; i < mTopCategories.size(); i++){     
    Category c = mTopCategories.get(i);
    TableRow row = ((TableRow)findViewById(rowID[idIdx])) ;
    cat = (TextView) getLayoutInflater().inflate(R.layout.cat_grid_item, null);
    int iconID = getResources().getIdentifier(c.getSlug().replace('-', '_'), "drawable", getPackageName());
    cat.setCompoundDrawablesWithIntrinsicBounds(0, iconID, 0, 0);
    int textID = getResources().getIdentifier(c.getSlug().replace('-', '_'), "string", getPackageName());
    cat.setText(textID);
    cat.setTag(c.getId().toString());
    row.addView(cat);
    }

最佳答案

尝试像这样在 TextView 上显式设置布局参数:

TableRow.LayoutParams lParams = new TableRow.LayoutParams(0, -2 /* WRAP_CONTENT */, 0.33f /* 33% of width */);
cat.setLayoutParams(lParams);

参见 http://developer.android.com/reference/android/widget/TableRow.LayoutParams.html#TableRow.LayoutParams%28int,%20int,%20float%29了解更多信息。

关于android - TableRow 中以编程方式添加的元素的权重不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24325420/

相关文章:

java - 未知错误: Didn't find class on path: DexPathList

android - 尝试将 App Bundle 上传到 Play 商店时出错

Android - 如何设置所有屏幕的背景颜色?

android - 是否可以将 TableLayout 与 ArrayAdapter 绑定(bind)?

android - TableLayout 中单击事件的问题

java - Android - fragment View 在 onCreateView 之外获取 NULL

android - 如何完全隐藏 ExpandableListView 的 groupIndicator?

java - 自定义 LinearLayout 中的子项不显示波纹效果

android - 如何在android中创建一个圆形imageButton?

java - 表格布局 : Text in the last column is not getting displayed completly