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

13

我需要相对布局/线性布局的运行时尺寸。

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);
请分享您的想法。
6个回答

18

试试这个

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

谢谢,它完美地运行了,我解决了我的问题,接受答案。非常感谢。 - Hiren Patel
1
接受并关闭问题。谢谢。 - user2511882
2
在您回答的时间后7分钟内我无法接受答案,请等待2分钟 :) - Hiren Patel

7

我是这样实现的:

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

    } 
});

这个例子是关于线性布局的,对于相对布局也是同样的过程。

希望这能帮到你。


你能获取宽度吗? - Nishant Rajput
@N.Droid,是的。两个都可以。 - Hiren Patel

5

您可以将 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);
                    }
                });

0

可以试试这样写:

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

    }

}

0

当活动刚创建时,由于布局尚未完全填充,因此我们可能无法立即获取视图或布局的正确高度和宽度。

所以

1.您可以让一个可运行的方法在几秒钟后触发,然后调用一个获取宽度和高度属性的函数。

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

为什么你回答了@vipul已经发布过的相同内容。这个地方不应该复制回答,而是如果你的答案类似的话可以点赞。 - Piyush Agarwal
抱歉,答案是我正在输入的同时,没有复制它 - 谢谢。 - Aromal Sasidharan

0

对于 Kotlin:您可以使用 AndroidX 的 doOnLayout 扩展函数来获取布局的高度或宽度。

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

当此视图被布局时执行给定的操作。如果视图已经被布局并且没有请求布局,则该操作将立即执行,否则该操作将在下次布局后执行。
该操作仅在下一次布局时调用一次,然后被移除。了解更多信息

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接