我有一个包含两个textview的列表视图。一个用于日期,一个用于名称。 Config.xml包含listview:
<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>
我还有两个按钮可以相互更改文本视图的内容,但由于第一个(最初)的内容比第二个更宽,我需要在代码中设置布局的宽度。 不幸的是没有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 :(得分:1)
您希望使用TextView的方法setLayoutParams
并将new LinearLayout.LayoutParams(width, height)
传递给它,其中width和height是将指定宽度和高度的整数。你可以保持高度并将宽度设置为你想要的任何东西。