Android Set the width of the text layout.

I have a list consisting of two text views. One for dates, one for names. Config.xml includes a listview:

<ListView android:id="@+id/ListView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/linlay"> </ListView> 

Row.xml contains two text images:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:paddingTop="4dip" android:paddingBottom="6dip" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <LinearLayout android:orientation="horizontal" android:id="@+id/linlay" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/left" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/right" android:layout_width="wrap_content" android:layout_height="fill_parent"/> </LinearLayout> </LinearLayout> 

I have two more buttons for changing the contents of text views with each other, but since the contents of the first (initially) are wider than the second, I need to set the width of the layout in the code. Unfortunately, there is no txt.setLayoutWidth or similar command, and txt.setWidth () does not work. I need something like this in this code:

  Button sortname = (Button)findViewById(R.id.Button02); sortname.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { lv1.setAdapter(simpleAdapter2); LinearLayout.LayoutParams layoutparams = new LinearLayout.LayoutParams(200,120); txt1.setLayoutParams(layoutparams); } }); 

These two lines cause the application to freeze (stops unexpectedly):

 LinearLayout.LayoutParams layoutparams = new LinearLayout.LayoutParams(200,120); txt1.setLayoutParams(layoutparams); 

Logcat:

 02-05 22:58:11.040: INFO/ActivityManager(58): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.bfarago.nevnap/.MainActivity } 02-05 22:58:11.210: DEBUG/AndroidRuntime(415): Shutting down VM 02-05 22:58:11.230: DEBUG/dalvikvm(415): Debugger has detached; object registry had 1 entries 02-05 22:58:11.330: INFO/AndroidRuntime(415): NOTE: attach of thread 'Binder Thread #3' failed 02-05 22:58:12.310: INFO/ActivityManager(58): Displayed activity com.bfarago.nevnap/.MainActivity: 1108 ms (total 1108 ms) 02-05 22:58:17.560: DEBUG/dalvikvm(125): GC_EXPLICIT freed 1274 objects / 73520 bytes in 185ms 02-05 22:58:17.660: DEBUG/AndroidRuntime(405): Shutting down VM 02-05 22:58:17.660: WARN/dalvikvm(405): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 02-05 22:58:17.670: ERROR/AndroidRuntime(405): FATAL EXCEPTION: main 02-05 22:58:17.670: ERROR/AndroidRuntime(405): java.lang.NullPointerException 02-05 22:58:17.670: ERROR/AndroidRuntime(405): at com.bfarago.nevnap.MainActivity$2.onClick(MainActivity.java:129) 02-05 22:58:17.670: ERROR/AndroidRuntime(405): at android.view.View.performClick(View.java:2408) 02-05 22:58:17.670: ERROR/AndroidRuntime(405): at android.view.View$PerformClick.run(View.java:8816) 02-05 22:58:17.670: ERROR/AndroidRuntime(405): at android.os.Handler.handleCallback(Handler.java:587) 02-05 22:58:17.670: ERROR/AndroidRuntime(405): at android.os.Handler.dispatchMessage(Handler.java:92) 02-05 22:58:17.670: ERROR/AndroidRuntime(405): at android.os.Looper.loop(Looper.java:123) 02-05 22:58:17.670: ERROR/AndroidRuntime(405): at android.app.ActivityThread.main(ActivityThread.java:4627) 02-05 22:58:17.670: ERROR/AndroidRuntime(405): at java.lang.reflect.Method.invokeNative(Native Method) 02-05 22:58:17.670: ERROR/AndroidRuntime(405): at java.lang.reflect.Method.invoke(Method.java:521) 02-05 22:58:17.670: ERROR/AndroidRuntime(405): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 02-05 22:58:17.670: ERROR/AndroidRuntime(405): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 02-05 22:58:17.670: ERROR/AndroidRuntime(405): at dalvik.system.NativeStart.main(Native Method) 02-05 22:58:17.680: WARN/ActivityManager(58): Force finishing activity com.bfarago.nevnap/.MainActivity 02-05 22:58:18.200: WARN/ActivityManager(58): Activity pause timeout for HistoryRecord{43e9a940 com.bfarago.nevnap/.MainActivity} 02-05 22:58:19.790: INFO/Process(405): Sending signal. PID: 405 SIG: 9 02-05 22:58:19.820: INFO/ActivityManager(58): Process com.bfarago.nevnap (pid 405) has died. 02-05 22:58:19.820: WARN/ActivityManager(58): Scheduling restart of crashed service com.bfarago.nevnap/.UpdateService in 5000ms 02-05 22:58:19.820: INFO/WindowManager(58): WIN DEATH: Window{43fce1d8 com.bfarago.nevnap/com.bfarago.nevnap.MainActivity paused=false} 02-05 22:58:19.920: WARN/InputManagerService(58): Got RemoteException sending setActive(false) notification to pid 405 uid 10048 02-05 22:58:24.291: DEBUG/dalvikvm(216): GC_EXPLICIT freed 93 objects / 3736 bytes in 153ms 02-05 22:58:24.901: INFO/ActivityManager(58): Start proc com.bfarago.nevnap for service com.bfarago.nevnap/.UpdateService: pid=425 uid=10048 gids={1015} 02-05 22:58:28.966: WARN/ActivityManager(58): Activity destroy timeout for HistoryRecord{43e9a940 com.bfarago.nevnap/.MainActivity} 02-05 22:58:29.350: DEBUG/dalvikvm(263): GC_EXPLICIT freed 44 objects / 2032 bytes in 164ms 
+2
source share
1 answer

You want to use the setLayoutParams TextView method and pass new LinearLayout.LayoutParams(width, height) , where width and height are integers that will indicate the width and height. You can keep the height and set the width as you want.

+1
source

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


All Articles