如何在代码中设置LinearLayout的高度

如何在代码中设置LinearLayout的高度
main.xml声明
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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:
mLinearLayout.setLayoutParams(
        		new LinearLayout.LayoutParams(400, 200));
    方法1会报下面的异常    
   
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:    可以正确使用
       
 ViewGroup.LayoutParams lp;        
        lp= mLinearLayout.getLayoutParams();
        lp.width=400;
        lp.height=200;        
        mLinearLayout.setLayoutParams(lp);

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值