如何在代码中设置LinearLayout的高度
展开全部
如何在代码中设置LinearLayout的高度
main.xml声明
[html] view plain copy
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="xxx"
android:id="@+id/context"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/linear_layout"
android:orientation="vertical">
<TextView
android:id="@+id/hello_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/button"
android:text="Click me Hide System statusbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:track="@drawable/switch_track_light"
android:thumb="@drawable/switch_inner_light"
android:textOn="@string/empty"
android:textOff="@string/empty1"/>
</LinearLayout>
</FrameLayout>
java文件onCreate中设置LinearLayout的width和height
mLinearLayout = (LinearLayout) findViewById(R.id.linear_layout);
//方法1:
[java] view plain copy
mLinearLayout.setLayoutParams(
new LinearLayout.LayoutParams(400, 200));
方法1会报下面的异常
[plain] view plain copy
01-03 01:48:12.914: E/AndroidRuntime(3353): FATAL EXCEPTION: main
01-03 01:48:12.914: E/AndroidRuntime(3353): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
//方法2: 可以正确使用
[java] view plain copy
ViewGroup.LayoutParams lp;
lp= mLinearLayout.getLayoutParams();
lp.width=400;
lp.height=200;
mLinearLayout.setLayoutParams(lp);
main.xml声明
[html] view plain copy
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="xxx"
android:id="@+id/context"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/linear_layout"
android:orientation="vertical">
<TextView
android:id="@+id/hello_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/button"
android:text="Click me Hide System statusbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:track="@drawable/switch_track_light"
android:thumb="@drawable/switch_inner_light"
android:textOn="@string/empty"
android:textOff="@string/empty1"/>
</LinearLayout>
</FrameLayout>
java文件onCreate中设置LinearLayout的width和height
mLinearLayout = (LinearLayout) findViewById(R.id.linear_layout);
//方法1:
[java] view plain copy
mLinearLayout.setLayoutParams(
new LinearLayout.LayoutParams(400, 200));
方法1会报下面的异常
[plain] view plain copy
01-03 01:48:12.914: E/AndroidRuntime(3353): FATAL EXCEPTION: main
01-03 01:48:12.914: E/AndroidRuntime(3353): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
//方法2: 可以正确使用
[java] view plain copy
ViewGroup.LayoutParams lp;
lp= mLinearLayout.getLayoutParams();
lp.width=400;
lp.height=200;
mLinearLayout.setLayoutParams(lp);
大雅新科技有限公司
2024-11-19 广告
2024-11-19 广告
这方面更多更全面的信息其实可以找下大雅新。深圳市大雅新科技有限公司从事KVM延长器,DVI延长器,USB延长器,键盘鼠标延长器,双绞线视频传输器,VGA视频双绞线传输器,VGA延长器,VGA视频延长器,DVI KVM 切换器等,优质供应商,...
点击进入详情页
本回答由大雅新科技有限公司提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询