ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – 在TableRow下设置TextView的layout_weight

2019-10-07 00:25:54  阅读:455  来源: 互联网

标签:tablelayout android textview


这个问题实际上与这篇文章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,但它仍然无法正常工作.

谢谢,
artsylar

解决方法:

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

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>

标签:tablelayout,android,textview
来源: https://codeday.me/bug/20191006/1863426.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有