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

给你一囗甜甜゛ 提交于 2019-11-28 03:10:38

问题


I required runtime dimensions of Relative/Linear Layout.

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

Please share your thoughts.


回答1:


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



回答2:


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.

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



回答3:


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.




回答4:


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

    }

}



回答5:


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.

2.we can use view tree 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();
    }



回答6:


For Kotlin: You can use AndroidX's doOnLayout extension function to get height or width of a layout

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



来源:https://stackoverflow.com/questions/20547974/how-to-get-programmatically-width-and-height-of-relative-linear-layout-in-andr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!