相关疑难解决方法(0)

如何为线性布局元素设置setLayoutParams

如何设置setLayoutParams()LinearLayout元素.在MainActivity.java.我为set layout params编写了以下代码,并nullPointerException在第50行显示,最后我粘贴了Logcat文件.

layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 50);
imageview1.setLayoutParams(layoutParams); // line no 50

layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 50);
textview1.setLayoutParams(layoutParams);

layoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
listview01.setLayoutParams(layoutParams);
Run Code Online (Sandbox Code Playgroud)

这是我的xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stackFromBottom="true"
>
<ImageView
android:id="@+id/imageview1"
android:layout_width="wrap_content"
android:layout_height="50px"
android:background="@drawable/applicationbar"
android:layout_x="0px"
android:layout_y="0px"
>
</ImageView>
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="50px"
android:text="TextView"
android:layout_x="0px"
android:layout_y="55px"
>
</TextView>
<ListView android:id="@+id/ListView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10px" 
android:layout_marginRight="10px"
android:layout_marginTop="35px" 
android:layout_marginBottom="40px"
android:paddingLeft="0px"
android:paddingRight="0px" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

原木猫:

04-19 16:47:22.221: ERROR/AndroidRuntime(3437): Uncaught handler: thread …
Run Code Online (Sandbox Code Playgroud)

android android-layout

22
推荐指数
2
解决办法
8万
查看次数

标签 统计

android ×1

android-layout ×1