Java 动态设置textview的宽度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18786232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:02:46  来源:igfitidea点击:

set Width dynamically of textview

javaandroid

提问by HimanshuR

In my project, i need to draw rows of textview dynamically according to the data received to show it. To align them properly, i have used Table Row n xml, and called it in java code. Now, in their LayoutParam i have given MATCH_PARENT, but it wraps the text according to the length of data received. Now, i want to fix width of the fields for a tabular view. I do all this process in postExecute method. In this method, i used setWidth function to set it according to the width of header row element.Here, Sno is a view, while size is array containing width of all elements of headerRow.

在我的项目中,我需要根据收到的数据动态绘制文本视图行以显示它。为了正确对齐它们,我使用了 Table Row n xml,并在 java 代码中调用了它。现在,在他们的 LayoutParam 中,我给出了 MATCH_PARENT,但它根据收到的数据长度包装文本。现在,我想修复表格视图的字段宽度。我在 postExecute 方法中完成所有这些过程。在这个方法中,我使用 setWidth 函数根据标题行元素的宽度来设置它。这里,Sno 是一个视图,而 size 是包含 headerRow 的所有元素的宽度的数组。

 Sno.setTag(patient);
 Sno.setWidth(size[0]); 

But it didn't solve this problem, when i tried getWidth to see its width, it was showing its value 0. Then i tried to set this width using LinearLayout.LinearParams

但它并没有解决这个问题,当我尝试 getWidth 查看它的宽度时,它显示的值为 0。然后我尝试使用 LinearLayout.LinearParams 设置这个宽度

LinearLayout.LayoutParams params_sno = new LinearLayout.LayoutParams(size[0],
LinearLayout.LayoutParams.MATCH_PARENT);
Sno.setLayoutParams(params_sno);

but still no benefit, secondaly, if i remove MATCH_PARENT from width of LayoutParams, its width got increased from width of header row element. fields without data are invisible.

但仍然没有好处,其次,如果我从 LayoutParams 的宽度中删除 MATCH_PARENT,它的宽度会从标题行元素的宽度增加。没有数据的字段是不可见的。

采纳答案by Sanket Shah

Try this

尝试这个

TableRow row = new TableRow(activityContext);

            TableLayout.LayoutParams td_tr = new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

            row.setWeightSum(10);
            row.setLayoutParams(td_tr);
            row.setGravity(Gravity.CENTER);
            row.setBackgroundColor(Color.parseColor("#D2D2D2"));

            TextView tv= new TextView(activityContext);
            tv.setText(String.valueOf(i));
            tv.setTextAppearance(activityContext, style.tvBoldRow);
            tv.setLayoutParams(new TableRow.LayoutParams(0 , LayoutParams.WRAP_CONTENT, 1)); // Here you can set weight to your TextView.
            tv.setPadding(2, 2, 2, 2);
            tv.setGravity(Gravity.CENTER);
            row.addView(tv);

回答by shivang Trivedi

try this:

尝试这个:

      LinearLayout.LayoutParams Params1 = new LinearLayout.LayoutParams(15,50);
      tv.setLayoutParams(Params1);  

回答by Shruti

Add the following parameter

添加以下参数

android:weight = "1"

android:weight = "1"

in xml to the textview and set

在 xml 到 textview 并设置

android:layout_width = "0dp".

android:layout_width = "0dp".

I think this should work for you.

我认为这应该对你有用。

Yes you can set weight from the java code.

是的,您可以从 java 代码设置权重。

To set weight from java code :

从java代码设置权重:

LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
                             LayoutParams.MATCH_PARENT,
                             LayoutParams.MATCH_PARENT, 1.0f);

The last parameter is the weight.

最后一个参数是权重。

回答by Pankaj

As tsp said

正如茶匙所说

LinearLayout.LayoutParams Params1 = new LinearLayout.LayoutParams(15,50);

then while adding it in table layout you have overrided addmethode which takes view and layout params.

然后在将它添加到表格布局中时,您已经覆盖了add采用视图和布局参数的方法。

You can also consider TypedValue.applyDimention() to give values in DP

您还可以考虑 TypedValue.applyDimention() 在 DP 中给出值

LinearLayout.LayoutParams Params1 = new LinearLayout.LayoutParams(TaypedValue.applyDimension(TaypedValue.COMPLEX_UNIT_DIP, 15, objDeisplayMetrics),TaypedValue.applyDimension(TaypedValue.COMPLEX_UNIT_DIP, 50, objDeisplayMetrics));