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



所以,我在一个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);

Try this way,you even no required any xml.
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TableLayout table = new TableLayout(this);
        table.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
        int id=1;
        for (int i=0;i<4;i++){
            TableRow tableRow = new TableRow(this);
            tableRow.setOrientation(TableLayout.HORIZONTAL);
            for(int j=0;j<3;j++){
                TextView textView = new TextView(this);
                textView.setGravity(Gravity.CENTER);
                textView.setId(id++);
                textView.setText(String.valueOf(textView.getId()));
                textView.setTag(textView.getId());
                textView.setTextColor(Color.WHITE);
                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(context, "Id is : " + v.getId(), Toast.LENGTH_SHORT).show();
                    }
                });
                textView.setBackgroundColor(Color.BLUE);
                TableRow.LayoutParams textviewParams = new TableRow.LayoutParams(0, TableRow.LayoutParams.MATCH_PARENT,1f);
                textviewParams.setMargins(5, 5, 5, 5);
                tableRow.addView(textView,textviewParams);
            }
            table.addView(tableRow,new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,0,1f));
        }
        setContentView(table);
    }

最新更新