Android Set textview layout width dynamically

谁都会走 提交于 2020-01-21 08:56:43

问题


I have a listview that consists of two textviews. One for dates, one for names. Config.xml includes the listview:

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

Row.xml includes the two textviews:

<?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 to change the content of the textviews with each other but as the content of the first one (initially) is wider than the second one, i need to set the width of the layout in the code. Unfortunately there is no txt.setLayoutWidth or similar command, and txt.setWidth() is not working. 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);
    }
  });

This two rows makes the app freeze (stopped 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

回答1:


You want to use the method setLayoutParams of TextView and pass into it a new LinearLayout.LayoutParams(width, height) where width and height are integers that will specify the width and height. you can keep the height and set the width to whatever you want.



来源:https://stackoverflow.com/questions/4909301/android-set-textview-layout-width-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!