2011-02-05 164 views
2

我有一個由兩個文字視圖組成的列表視圖。一個用於日期,一個用於名稱。 的Config.xml包括列表視圖:Android動態設置textview佈局寬度

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

Row.xml包括兩個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> 

我有兩個更多的按鈕來改變textviews的內容彼此但是作爲內容第一個(最初)比第二個寬,我需要在代碼中設置佈局的寬度。 不幸的是,沒有txt.setLayoutWidth或類似的命令,並且txt.setWidth()不起作用。 我需要的東西像這樣的代碼:

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); 
    } 
    }); 

這兩行,使應用程序凍結(意外停止):

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 
+0

txt1從哪裏來?似乎它是空的。 – 2013-06-15 18:23:23

回答

1

你想使用方法setLayoutParams的TextView並傳遞給它一個new LinearLayout.LayoutParams(width, height)其中寬度和高度是指定寬度和高度的整數。你可以保持高度,並將寬度設置爲任何你想要的。

+0

我如你所見修改了row.xml文件。而這段代碼凍結了我的應用,但是根據我的例子我很接近。 \t LinearLayout.LayoutParams layoutparams = new LinearLayout.LayoutParams(200,120); \t \t txt1.setLayoutParams(layoutparams); – erdomester 2011-02-05 21:30:42