如何在 android 中获取 wrap_content 或 match_parent 布局宽度和高度?
How can I get wrap_content or match_parent layout width and height in android?
我想在某些 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"/>
我有这个布局,我想在运行时获取布局的宽度和高度大小(倾斜),我该怎么做?
我想你可以使用像
这样的东西
LinearLayout myView =(LinearLayout)findViewById(R.id.layout_scrol_left);
并且在 UI 线程已确定大小并铺设后,您可以使用下面的方法
myview.getHeight();
myview.getWidth();
希望这对您有所帮助。
如果您在使用上述方法时遇到问题,请查看 getWidth() and getHeight() of View returns 0
首先你需要进行布局。
LinearLayout ll_left = (LinearLayout)findViewById(R.id.layout_scrol_left);
现在,如果 ll_left 尚未绘制,ll_left.getHeight() 和 ll_left.getWidth() 都将 return 0。
所以你必须在绘制视图后获取它们:
ll_left.post(new Runnable(){
public void run(){
int height = ll_left.getHeight();
int weight = ll_left.getWidth();
}
});
我想在某些 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"/>
我有这个布局,我想在运行时获取布局的宽度和高度大小(倾斜),我该怎么做?
我想你可以使用像
这样的东西 LinearLayout myView =(LinearLayout)findViewById(R.id.layout_scrol_left);
并且在 UI 线程已确定大小并铺设后,您可以使用下面的方法
myview.getHeight();
myview.getWidth();
希望这对您有所帮助。
如果您在使用上述方法时遇到问题,请查看 getWidth() and getHeight() of View returns 0
首先你需要进行布局。
LinearLayout ll_left = (LinearLayout)findViewById(R.id.layout_scrol_left);
现在,如果 ll_left 尚未绘制,ll_left.getHeight() 和 ll_left.getWidth() 都将 return 0。
所以你必须在绘制视图后获取它们:
ll_left.post(new Runnable(){
public void run(){
int height = ll_left.getHeight();
int weight = ll_left.getWidth();
}
});