我想在某些 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/