问题描述
我有一个由两个textviews的列表视图。一个日期,一个名字。
config.xml中包括列表视图:
< ListView的机器人:ID =@ + ID / ListView01
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ + ID / linlay>
< /&的ListView GT;
Row.xml包括两个textviews:
<?XML版本=1.0编码=UTF-8&GT?;<的LinearLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:paddingTop =4dip
机器人:paddingBottom会=6DIP
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向>
<的LinearLayout机器人:方向=横向
机器人:ID =@ + ID / linlay
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT>
< TextView的机器人:ID =@ + ID /左
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
/> < TextView的机器人:ID =@ + ID /右
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =FILL_PARENT/>
< / LinearLayout中>
< / LinearLayout中>
我有两个按钮来改变textviews的内容相互但作为第一种的含量(最初)比第二个更广泛的,我需要设置布局的宽度在code。
不幸的是没有txt.setLayoutWidth或类似的命令,txt.setWidth()不工作。
我需要这样的事情在这个code:
按钮sortname =(按钮)findViewById(R.id.Button02);
sortname.setOnClickListener(新View.OnClickListener()
{
公共无效的onClick(视图v)
{
lv1.setAdapter(simpleAdapter2);
LinearLayout.LayoutParams的LayoutParams =新LinearLayout.LayoutParams(200120);
txt1.setLayoutParams(的LayoutParams);
}
});
这两行,使应用程序冻结(意外停止):
LinearLayout.LayoutParams的LayoutParams =新LinearLayout.LayoutParams(200120);
txt1.setLayoutParams(的LayoutParams);
logcat的:
02-05 22:58:11.040:信息/ ActivityManager(58):启动活动:意向{行动= android.intent.action.MAIN猫= [android.intent.category .LAUNCHER] FLG = 0x10000000的CMP = com.bfarago.nevnap / .MainActivity}
02-05 22:58:11.210:DEBUG / AndroidRuntime(415):关闭VM
02-05 22:58:11.230:DEBUG / dalvikvm(415):调试器分离;对象注册了1项
02-05 22:58:11.330:信息/ AndroidRuntime(415):注:连接线的捆扎线#3'失败
02-05 22:58:12.310:信息/ ActivityManager(58):显示活动com.bfarago.nevnap / .MainActivity:1108毫秒(共1108毫秒)
02-05 22:58:17.560:DEBUG / dalvikvm(125):GC_EXPLICIT释放1274对象/在185ms 73520字节
02-05 22:58:17.660:DEBUG / AndroidRuntime(405):关闭VM
02-05 22:58:17.660:WARN / dalvikvm(405):主题ID = 1:螺纹未捕获的异常退出(组= 0x4001d800)
02-05 22:58:17.670:ERROR / AndroidRuntime(405):致命异常:主要
02-05 22:58:17.670:ERROR / AndroidRuntime(405):显示java.lang.NullPointerException
02-05 22:58:17.670:ERROR / AndroidRuntime(405):在com.bfarago.nevnap.MainActivity $ 2.onClick(MainActivity.java:129)
02-05 22:58:17.670:ERROR / AndroidRuntime(405):在android.view.View.performClick(View.java:2408)
02-05 22:58:17.670:ERROR / AndroidRuntime(405):在android.view.View $ PerformClick.run(View.java:8816)
02-05 22:58:17.670:ERROR / AndroidRuntime(405):在android.os.Handler.handleCallback(Handler.java:587)
02-05 22:58:17.670:ERROR / AndroidRuntime(405):在android.os.Handler.dispatchMessage(Handler.java:92)
02-05 22:58:17.670:ERROR / AndroidRuntime(405):在android.os.Looper.loop(Looper.java:123)
02-05 22:58:17.670:ERROR / AndroidRuntime(405):在android.app.ActivityThread.main(ActivityThread.java:4627)
02-05 22:58:17.670:ERROR / AndroidRuntime(405):在java.lang.reflect.Method.invokeNative(本机方法)
02-05 22:58:17.670:ERROR / AndroidRuntime(405):在java.lang.reflect.Method.invoke(Method.java:521)
02-05 22:58:17.670:ERROR / AndroidRuntime(405):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
02-05 22:58:17.670:ERROR / AndroidRuntime(405):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-05 22:58:17.670:ERROR / AndroidRuntime(405):在dalvik.system.NativeStart.main(本机方法)
02-05 22:58:17.680:WARN / ActivityManager(58):强制完成活动com.bfarago.nevnap / .MainActivity
02-05 22:58:18.200:WARN / ActivityManager(58):为HistoryRecord活动暂停超时{43e9a940 com.bfarago.nevnap / .MainActivity}
02-05 22:58:19.790:信息/流程(405):发送信号。 PID:405 SIG:9
02-05 22:58:19.820:信息/ ActivityManager(58):过程com.bfarago.nevnap(PID 405)已经死亡。
02-05 22:58:19.820:在5000毫秒坠毁服务com.bfarago.nevnap调度重启/ .UpdateService:WARN / ActivityManager(58)
02-05 22:58:19.820:信息/窗口管理器(58):WIN死亡:窗口{43fce1d8 com.bfarago.nevnap / com.bfarago.nevnap.MainActivity暂停= FALSE}
02-05 22:58:19.920:WARN / InputManagerService(58):GOT的RemoteException发送SETACTIVE(假)的通知为PID 405 UID 10048
02-05 22:58:24.291:DEBUG / dalvikvm(216):GC_EXPLICIT释放在153ms 93的对象/ 3736字节
02-05 22:58:24.901:信息/ ActivityManager(58):启动PROC com.bfarago.nevnap服务com.bfarago.nevnap / .UpdateService:PID = 425 UID = 10048导报= {} 1015
02-05 22:58:28.966:WARN / ActivityManager(58):活动破坏超时HistoryRecord {43e9a940 com.bfarago.nevnap / .MainActivity}
02-05 22:58:29.350:DEBUG / dalvikvm(263):GC_EXPLICIT释放在164ms 44的对象/ 2032字节
您要使用的方法 setLayoutParams
的TextView,并传递到它新LinearLayout.LayoutParams(宽,高)
,其中宽度和高度都将指定宽度和高度的整数。你可以保持高度和设置宽度为任何你想要的东西。
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
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.
这篇关于Android的设置TextView的宽度布局动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!