如何获得在android的相对线性布局的程序的宽度和高度?

我需要相对/线性布局的运行时间维度。

activity_home.xml

<RelativeLayout android:id="@+id/rlParent" android:layout_width="match_parent" android:layout_height="match_parent" > </RelativeLayout> 

HomeActivity.java:

 private int width, height; RelativeLayout rlParent = (RelativeLayout) findViewById(R.id.rlParent); w = rlParent.getWidth(); h = rlParent.getHeight(); Log.i("Width-Height", width+"-"+height); 

请分享你的想法。

Solutions Collecting From Web of "如何获得在android的相对线性布局的程序的宽度和高度?"

尝试这个

 @Override public void onWindowFocusChanged(boolean hasFocus) { // TODO Auto-generated method stub super.onWindowFocusChanged(hasFocus); updateSizeInfo(); } private void updateSizeInfo() { RelativeLayout rl_cards_details_card_area = (RelativeLayout) findViewById(R.id.rl_cards_details_card_area); w = rl_cards_details_card_area.getWidth(); h = rl_cards_details_card_area.getHeight(); Log.v("WH", w+"-"+h); } 

您可以将OnGlobalLayoutListener附加到相对布局的ViewTreeObserver,当视图附加到窗口时将调用相关布局,并将其实际高度分配给它。

 final RelativeLayout rl_cards_details_card_area = (RelativeLayout) findViewById(R.id.rl_cards_details_card_area); rl_cards_details_card_area.getViewTreeObserver() .addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { // TODO Auto-generated method stub int w = rl_cards_details_card_area.getWidth(); int h = rl_cards_details_card_area.getHeight(); Log.v("WH", w + "-" + h); rl_cards_details_card_area.getViewTreeObserver() .removeOnGlobalLayoutListener(this); } }); 

那么我已经通过这种方式实现了:

 LinearLayout linearLayout = (LinearLayout)findViewById(R.id.layout); ViewTreeObserver viewTreeObserver = linearLayout.getViewTreeObserver(); viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { linearLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this); int width = linearLayout.getMeasuredWidth(); int height = linearLayout.getMeasuredHeight(); } }); 

这个例子适用于线性布局, 相对布局遵循相同的过程。

希望这会帮助你。

尝试像这样:

 import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.TextView; public class AndroidResizeView extends Activity { TextView textMyTextView; Button myButton; RelativeLayout rlayout; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.hightk); rlayout = (RelativeLayout) findViewById(R.id.rlayout); textMyTextView = (TextView) findViewById(R.id.textView1); myButton = (Button) findViewById(R.id.button1); myButton.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub updateSizeInfo(); } }); } @Override public void onWindowFocusChanged(boolean hasFocus) { // TODO Auto-generated method stub super.onWindowFocusChanged(hasFocus); updateSizeInfo(); } private void updateSizeInfo() { textMyTextView.setText(String.valueOf("Width: " + rlayout.getWidth() + "height ::" + rlayout.getHeight())); } } 

我们可能无法得到正确的高度和视图或布局的活力刚刚创build,因为布局没有完全膨胀

所以

你可以让一个runnable方法触发一些secs,然后调用一个获取width和height属性的函数。

我们可以使用视图树obsever

  l = (RelativeLayout)findviewbyid(R.id.rl_cards_details_card_area); ViewTreeObserver observer = l.getViewTreeObserver(); observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { // TODO Auto-generated method stub ActivityClassName.this.getProperties(); l.getViewTreeObserver().removeGlobalOnLayoutListener( this); } }); void getProperties() { int a= l.getHeight(); int b = l.getWidth(); Toast.makeText(getActivity,""+a+" "+b,1000).show(); }