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

标签 android height width

我想在某些 Android 设备上获取我的布局或 View (不是屏幕尺寸)的宽度和高度,那么我该怎么做呢?例如:

<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"/>

我有这个布局,我想在运行时获取布局的宽度和高度大小(倾斜),我该怎么做?

最佳答案

首先,您需要进行布局。

LinearLayout ll_left = (LinearLayout)findViewById(R.id.layout_scrol_left);

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

所以你必须在绘制 View 后获取它们:

ll_left.post(new Runnable(){
    public void run(){
       int height = ll_left.getHeight();
       int weight = ll_left.getWidth();
    }
});

关于android - 如何在 android 中获取 wrap_content 或 match_parent 布局宽度和高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40691174/

相关文章:

height - 如何更改 CKEditor 工具栏的大小?

CSS 百分比高度和自动宽度不在 IE 中创建纵横比

CSS 高度和宽度属性在 IE10 中无法正常工作

html - 如何将 HTML 表格宽度限制为屏幕宽度的 95%?

android - Gradle构建文件的Freemarker模板

android - 无法启动用于广播 Intent 的应用程序

listview - Xamarin.Forms ListView 大小适合内容吗?

html - 如何使 div 动态更改为内容宽度并使其保持该宽度,即使浏览器窗口大小发生变化?

Android LocationServices.FusedLocationApi.getLastLocation 返回 "null"

android - 从 Android 的 ScrollView 中删除滚动条轨道