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


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

虽然有效果了,可是假设里面的内容过多的话,这个布局就不起作用了。

所以我们依据手机屏幕的大小  动态的设置它的高度。

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;
			}
		});
	}

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

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

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-04
  • 2022-01-08
  • 2022-01-08
  • 2022-03-01
  • 2022-01-29
  • 2022-01-08
猜你喜欢
  • 2021-10-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-08
相关资源
相似解决方案