如何在android中获得wrap_content或match_parent布局的宽度和高度?

b4h*_*r4m 2 height android width

我想在一些Android设备中获取我的布局或视图(不是屏幕尺寸)宽度,所以我该怎么做?例如:

<LinearLayout
     android:orientation="vertical"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:layout_weight="1"
     android:paddingRight="8dp"
     android:paddingLeft="8dp"
     android:id="@+id/layout_scrol_left"/>
Run Code Online (Sandbox Code Playgroud)

我有这个布局,我想在运行时获得布局宽度和高度尺寸(在倾角),我该怎么做?

Dan*_* D. 5

首先,你需要采取你的布局.

LinearLayout ll_left = (LinearLayout)findViewById(R.id.layout_scrol_left);
Run Code Online (Sandbox Code Playgroud)

现在,如果尚未绘制ll_left,则ll_left.getHeight()和ll_left.getWidth()将返回0.

所以你必须在绘制视图后得到它们:

ll_left.post(new Runnable(){
    public void run(){
       int height = ll_left.getHeight();
       int weight = ll_left.getWidth();
    }
});
Run Code Online (Sandbox Code Playgroud)

  • 谢谢 .我的问题出来了:) (2认同)