[英]Get width of linear/relative layout?
Is it possible to get the width of a linear/relative layout, if the layout_width is set to fill_parent in the layout xml? 如果在layout xml中将layout_width设置为fill_parent,是否可以获得线性/相对布局的宽度? For example, I have the following layout xml. 例如,我有以下布局xml。 Is it possible to find the width of the cameraSourceScreen? 是否可以找到cameraSourceScreen的宽度? Thanks in advance :) 提前致谢 :)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/CameraSourcesScreen"
>
<LinearLayout android:id="@+id/button_bar"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:background="@drawable/topbar"
android:orientation="horizontal"
android:gravity="center">
<LinearLayout android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal" android:paddingLeft="20dp">
<common.view.ToggleButton android:id="@+id/Near_Toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="2dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" android:text="@string/NEAR_STR"
android:textSize="18px" android:width="130dp"/>
<common.view.ToggleButton android:id="@+id/Far_Toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="2dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" android:text="@string/FAR_STR"
android:textSize="18px" android:width="130dp"/>
</LinearLayout> </LinearLayout> </RelativeLayout>
You can add an android:id to your RelativeLayout
, then call findViewById
as you do for the rest of the widgets and then use the getWidth method. 您可以将android:id添加到您的RelativeLayout
,然后像对待其他小部件一样调用findViewById
,然后使用getWidth方法。 Though... it seems that your RelativeLayout
is filling all the screen, so why don't you just try to know the screen dimensions? 虽然...看起来您的RelativeLayout
似乎填满了整个屏幕,但是为什么不尝试了解屏幕尺寸呢?
Display display = getWindowManager().getDefaultDisplay();
display.getWidth();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.