【问题标题】:Setting the layout_weight of the TextView under the TableRow设置TableRow下TextView的layout_weight
【发布时间】:2011-10-05 02:47:57
【问题描述】:

这个问题其实和这个帖子有关Set the layout weight of a TextView programmatically

根据答案,我只需要如下设置 TextView 布局参数并设置 stretchColumn 属性,但是通过添加以下代码,它会使 textView 从表格布局中消失。

TextView tv = new TextView(v.getContext());
tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));

所以,这是我的代码,我想动态添加一行,其中包含 3 列,权重分别为 35%、5% 和 60%。请让我知道我的代码有什么问题。

private void addTableRow(int resIdTitle, String strValue){

        TableRow tr = new TableRow(this);

        // Add First Column
        TextView tvTitle = new TextView(this);
        tvTitle.setText(resIdTitle);
        tvTitle.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 
                LayoutParams.WRAP_CONTENT, 0.35f));
        tr.addView(tvTitle);

        // Add 2nd Column
        TextView tvColon = new TextView(this);
        tvColon.setText(" : ");
        tvColon.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 
                LayoutParams.WRAP_CONTENT, 0.05f));
        tr.addView(tvColon);

        // Add the 3rd Column
        TextView tvValue = new TextView(this);
        tvValue.setText(strValue);
        tvValue.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 
                LayoutParams.WRAP_CONTENT, 0.60f));
        tr.addView(tvValue);

        // Finally add it to the table
        tl.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        Log.d(TAG, "Row Added");
    }

这是我的 xml 文件,

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">
    <TableLayout
      android:id="@+id/tl_cam_get_param"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:title="@string/strGetParamTitle" 
      android:scrollbars="vertical"       
      android:shrinkColumns="0">
        </TableLayout>
</ScrollView>

我也试过将 layout_width 设置为 0,但还是不行。

谢谢,
艺人

【问题讨论】:

    标签: android textview tablelayout


    【解决方案1】:

    谢谢大家! 最后,我能够通过参考以下帖子来解决我的问题。

    android: two issues using Tablerow+TextView in Tablelayout

    How to make a TableLayout from XML using programmable way ?

    请参阅下面的我的工作代码。

    这是动态添加行的地方。

    TableLayout tl = (TableLayout)findViewById(R.id.table_layout);
    
    private void addTableRow(int resIdTitle, String strValue){
    
        LayoutInflater inflater = getLayoutInflater();
        TableRow tr = (TableRow)inflater.inflate(R.layout.table_row, tl, false);
    
        // Add First Column
        TextView tvTitle = (TextView)tr.findViewById(R.id.tvTitle);
        tvTitle.setText(resIdTitle);
    
        // Add the 3rd Column
        TextView tvValue = (TextView)tr.findViewById(R.id.tvValue);
        tvValue.setText(strValue);
    
        tl.addView(tr);
    }
    

    这是 table_layout.xml

     <?xml version="1.0" encoding="utf-8"?>
    <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">
        <TableLayout
          android:id="@+id/table_layout"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:title="@string/strGetParamTitle" 
          android:scrollbars="vertical"       
          android:shrinkColumns="0">
        </TableLayout>
    </ScrollView>
    

    最后是 table_row.xml。我在这里设置了所有的 layout_params。

    <?xml version="1.0" encoding="utf-8"?>
    <TableRow xmlns:android="http://schemas.android.com/apk/res/android">
       <TextView  
              android:id="@+id/tvTitle"
              android:paddingRight="3dip"
              android:layout_width="0px"
              android:layout_weight="0.35"/>
       <TextView  android:text=":"
              android:layout_width="0px"
              android:layout_weight="0.05"/>
       <TextView
                android:id="@+id/tvValue"
                android:paddingLeft="3dip"
                android:layout_width="0px"
                android:layout_weight="0.6"
              /> 
    </TableRow>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-02
      • 1970-01-01
      • 1970-01-01
      • 2012-12-29
      • 1970-01-01
      • 2011-09-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多