使用relativelayout并排添加控件以占据整个屏幕宽度

时间:2014-03-17 07:50:32

标签: android relativelayout

Android Studio 0.5.1

您好,

我将layout_width设置为200dp的屏幕截图。但这不是一个好的解决方案,因为这对屏幕尺寸来说太大或太小。我想两者兼顾,以便搜索栏只占用最终适合textview的空间。 enter image description here

是否可以使用relativelayout将控件并排放置,其中搜索栏将占用足够的空间而不会将控件重叠到其右侧(TextView)?

我希望seekbar和textview能够将整个宽度放在一起。

对于搜索栏我使用了匹配父级。然而,这推动了文本视图的消失。

我希望搜索栏停在textview开始的位置。我不想在layout_width上使用dp,因为每个设备都不同。

我不想切换到线性布局,因为我有其他使用此布局的控件(此处未显示)。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

  <SeekBar
        android:id="@+id/sbSensitivity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="15"
    android:layout_alignParentLeft="true"/>

    <TextView
        android:id="@+id/tvSensitivityDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:text="15"
        android:layout_toRightOf="@+id/sbSensitivity"
    android:layout_alignParentRight="true"/>

</RelativeLayout>

我希望你理解我的意思?

非常感谢任何建议,

2 个答案:

答案 0 :(得分:1)

试试这个

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="wrap_content">
 <SeekBar
        android:id="@+id/sbSensitivity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:max="15"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/tvSensitivityDisplay"/>

    <TextView
        android:id="@+id/tvSensitivityDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:text="15"
    android:layout_alignParentRight="true"/>

</RelativeLayout>

答案 1 :(得分:1)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content" android:weightSum="2">

  <SeekBar
        android:id="@+id/sbSensitivity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="15"
       android:layout_weight="1"        
    />

    <TextView
        android:id="@+id/tvSensitivityDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"
      android:layout_weight="1"       
       android:text="15"
     />
</LinearLayout>
</RelativeLayout>
相关问题