android - 设置TableRow下TextView的layout_weight

标签 android textview tablelayout

这个问题其实和这个帖子有关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: 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>

关于android - 设置TableRow下TextView的layout_weight,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6689896/

相关文章:

android - 以编程方式将文本颜色设置为主要的 android textview

Android textview 大纲文本

android: 帮助创建表格布局

Android 模拟器无法正常工作

android - "Sign in to Wi-Fi Network"在 Android 设备上永远不会发生,但重定向工作正常

android - 如何很好地缩放 Android TextView 的背景图像

android - 代码似乎改变了我的布局文件

java - CountDownTimer 的连续函数

android - 如何避免 Android 中的 ProgressDialog

TableLayout 中的 Android EditText 跑掉了