简体   繁体   English

如何在android中以编程方式获取相对-线性布局的宽度和高度?

[英]How to get programmatically width and height of Relative - Linear layout in android?

I required runtime dimensions of Relative/Linear Layout.我需要相对/线性布局的运行时尺寸。

activity_home.xml :活动_home.xml :

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

HomeActivity.java:主页Activity.java:

private int width, height;

RelativeLayout rlParent = (RelativeLayout) findViewById(R.id.rlParent);

w = rlParent.getWidth();
h = rlParent.getHeight();

Log.i("Width-Height", width+"-"+height);

Please share your thoughts.请分享您的想法。

Try this试试这个

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    // TODO Auto-generated method stub
    super.onWindowFocusChanged(hasFocus);
    updateSizeInfo();
}

private void updateSizeInfo() {
    RelativeLayout rlayout = (RelativeLayout) findViewById(R.id.rlayout);
    w = rlayout.getWidth();
    h = rlayout.getHeight();
    Log.v("W-H", w+"-"+h);
}

You can attach a OnGlobalLayoutListener to ViewTreeObserver of the relative layout which will get called when the view is attached to the window and it's actual height is assigned to it.您可以将 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("W-H", w + "-" + h);
                        rl_cards_details_card_area.getViewTreeObserver()
                                .removeOnGlobalLayoutListener(this);
                    }
                });

Well I have implemented by this way:那么我已经通过这种方式实现了:

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

    } 
});

This example is for Linear layout, for Relative layout follow same process.这个例子是线性布局,相对布局遵循相同的过程。

Hope this would help you.希望这会帮助你。

try like this:试试这样:

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

    }

}

we may not get correct height and wight of a view or layout as soon the activity is just created since the layout is not inflated completly由于布局没有完全膨胀,我们可能无法在刚刚创建活动时获得视图或布局的正确高度和宽度

so所以

1.either you can let a runnable method triggered after some secs which in turn call a function that gets the width and height properties. 1.你可以让一个可运行的方法在几秒钟后触发,然后调用一个获取宽度和高度属性的函数。

2.we can use view tree obsever 2.我们可以使用视图树观察器

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

For Kotlin : You can use AndroidX's doOnLayout extension function to get height or width of a layout对于 Kotlin :您可以使用 AndroidX 的 doOnLayout 扩展功能来获取布局的高度或宽度

view.doOnLayout {
  Timber.d("View's Height: ${view.height}")
}

Performs the given action when this view is laid out.在布局此视图时执行给定的操作。 If the view has been laid out and it has not requested a layout, the action will be performed straight away, otherwise the action will be performed after the view is next laid out.如果视图已经布局并且还没有请求布局,则该操作将立即执行,否则将在下一次布局视图之后执行该操作。

The action will only be invoked once on the next layout and then removed.该动作只会在下一个布局上被调用一次,然后被删除。 For more info 欲了解更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM