Dynamic Typing Width

In my project, I need to dynamically draw lines of a text view according to the received data in order to show it. To align them correctly, I used Table Row n xml and named it in java code. Now, in their LayoutParam I gave MATCH_PARENT, but it wraps the text according to the length of the received data. Now I want to fix the width of the fields for the table view. I am doing this whole process in the postExecute method. In this method, I used the setWidth function to set it according to the width of the title bar element. Here, Sno is a view, and size is an array containing the width of all headerRow elements.

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

But this did not solve this problem, when I tried getWidth to see its width, it showed its value 0. Then I tried to set this width using 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 the width of the LayoutParams, its width has increased from the width of the title bar element. fields without data are invisible.

+6
source share
4 answers

try it

 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); 
+3
source

try the following:

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

Add the following parameter

android:weight = "1"

in xml to text view and set

android:layout_width = "0dp" .

I think this should work for you.

Yes, you can set the weight from Java code.

To set weight from Java code:

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

The last parameter is weight.

0
source

As tsp said

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

then when you add a table to the layout, you redefined add methode, which accepts view and layout options.

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

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

Source: https://habr.com/ru/post/953777/


All Articles