2010-11-19 79 views
0

如果布局xml中的layout_width设置为fill_parent,是否可以获得线性/相对布局的宽度?例如,我有以下布局xml。是否有可能找到cameraSourceScreen的宽度?感谢提前:)获取线性/相对布局的宽度?

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

回答

4

您可以添加一个机器人:ID您RelativeLayout,然后调用findViewById为您的小部件的休息做,然后使用方法的getWidth。虽然......看起来您的RelativeLayout正在填满整个屏幕,那么为什么不尝试了解屏幕尺寸呢?

Display display = getWindowManager().getDefaultDisplay(); 
display.getWidth(); 
+0

哇,不知道我能做到这一点...谢谢:) – iman453 2010-11-19 17:28:37

+0

它不会在Android ICS +填充屏幕,因为有在底部的图形按钮栏。 – Fresheyeball 2012-10-30 15:13:48