android在OnCreate中获取控件的宽度和高度.

一个demo,在xml布局中,某一行的高度为其他行的一半。之前用layout_weight,得出的效果也不太理想。

<?xml version="1.0" encoding="UTF-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
    android:layout_width="match_parent"   
    android:layout_height="match_parent" 
    android:orientation="vertical" >   
    
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        android:layout_weight="2"
        >
        <TextView   
        	android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
        	android:text="1"
        	android:textColor="@android:color/white"
        />
    </LinearLayout>
    
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
		android:background="#cccccc"
        android:layout_weight="2"
        >
        <TextView
        	android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
        	android:text="2"
        	android:textColor="@android:color/black" />
    </LinearLayout>
    
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
		android:background="#ddaacc"
        android:layout_weight="1"
        >
        <TextView
        	android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
        	android:text="3"
        	android:textColor="@android:color/black"
         />
    </LinearLayout>
     
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
		android:background="#000"
        android:layout_weight="1"
        >
        <TextView   
        	android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
        	android:text="4"
        	android:textColor="@android:color/white"  
        /> 
    </LinearLayout>
     
</LinearLayout> 

尽管有效果了,但是如果里面的内容过多的话,这个布局就不起作用了。所以我们根据手机屏幕的大小  动态的设置它的高度。

public class HomeActivity extends Activity{
	
	private LinearLayout homelayout; //父

	private LinearLayout home1;
	private LinearLayout home2;
	private LinearLayout home3;
	private LinearLayout home4;

	boolean hasMeasured = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
		
		home1 = (LinearLayout)findViewById(R.id.home_content_section1);
		home2 = (LinearLayout)findViewById(R.id.home_content_section2);
		home3 = (LinearLayout)findViewById(R.id.home_content_section3);
		home4 = (LinearLayout)findViewById(R.id.home_content_section4);
		homelayout = (LinearLayout)findViewById(R.id.home_content);

		ViewTreeObserver vto = homelayout.getViewTreeObserver();
		vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
			@Override
			public boolean onPreDraw() {
				if (hasMeasured == false)
                		{	
					//获取homelayout的宽度和高度
	                		int height = homelayout.getMeasuredHeight();
	                		//int width = homelayout.getMeasuredWidth();
	                		android.view.ViewGroup.LayoutParams lp1 =home1.getLayoutParams();
	                		lp1.height=height*2/7;
	                		android.view.ViewGroup.LayoutParams lp2 =home2.getLayoutParams();
	                		lp2.height=height*2/7;
	                		android.view.ViewGroup.LayoutParams lp3 =home3.getLayoutParams();
	                		lp3.height=height*1/7;
	                		android.view.ViewGroup.LayoutParams lp4 =home4.getLayoutParams();
	                		lp4.height=height*2/7;

	                		hasMeasured = true;
	                	}
				return true;
			}
		});
	}
 

欢迎交流 http://blog.csdn.net/ycwol/article/details/41279923

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值