Android相对布局,定位在2个视图中间

时间:2015-04-02 12:47:41

标签: android android-relativelayout

我在相对布局中有2个ImageButtons和一个Seerkbar,我试图将它们放置在以下位置:

1)第一个imageButton位于左侧,它位于左侧,父级左侧对齐。

2)第二个imageButton位于右侧,右侧是父级右侧对齐。

3)Seekbar位于中间,它覆盖了所有剩余的空间。

看起来应该是这样的:

enter image description here 我试过这个:

    <RelativeLayout android:layout_width="match_parent"
                android:layout_height="wrap_content">

    <ImageButton android:id="@+id/image_1"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_alignParentLeft="true"
                 android:src="@drawable/image_1"/>

    <SeekBar android:id="@+id/seek_bar"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:layout_toRightOf="@id/image_1"
             android:layout_toLeftOf="@id/image_2"
             android:max="95"/>

    <ImageButton android:id="@+id/image_2"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_alignParentRight="true"
                 android:src="@drawable/image_2"/>

</RelativeLayout>

这在我的intellij设备预览中工作正常,但由于以下行而无法编译:

          android:layout_toLeftOf="@id/image_2"

因为我在声明之前使用了id / image_2。有解决方案吗?

4 个答案:

答案 0 :(得分:1)

TRY THIS....


<RelativeLayout android:layout_width="match_parent"
                    android:layout_height="wrap_content">

        <ImageButton android:id="@+id/image_1"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_alignParentLeft="true"
                     android:src="@drawable/image_1"/>

        <SeekBar android:id="@+id/seek_bar"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:layout_toRightOf="@+id/image_1"
                 android:layout_toLeftOf="@+id/image_2"
                 />

        <ImageButton android:id="@+id/image_2"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_alignParentRight="true"
                     android:src="@drawable/image_2"/>

    </RelativeLayout>

答案 1 :(得分:1)

我认为您忘记在+之前添加id符号:

android:layout_toRightOf="@+id/image_1"
android:layout_toLeftOf="@+id/image_2"

答案 2 :(得分:0)

我不建议RelativeLayout引用toRightOftoLeftOf等两个组件。始终尝试仅将每个引用一个。在您的情况下,您还可以使用LinearLayout权重为chils的统一分发,如下所示

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<ImageButton
    android:id="@+id/image_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:src="@drawable/ic_launcher" />

<SeekBar
    android:id="@+id/seek_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:max="95" />

<ImageButton
    android:id="@+id/image_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:src="@drawable/ic_launcher" />

</LinearLayout>

答案 3 :(得分:0)

这可以帮助您处理不同的分辨率,试一试 我已将imageview代替seekbar lol

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="100"
android:gravity="center"
android:background="#FFF"
android:layout_margin="1dp">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="40"
    android:background="#aef"
    android:layout_margin="4dp">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView"
        android:background="#984"/>
</LinearLayout>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="100dip"
    android:layout_weight="20"
    android:background="#fec"
    android:layout_margin="4dp">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView2"
        android:background="#594"/>
</LinearLayout>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="40"
    android:background="#9ef"
    android:layout_margin="4dp">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/imageView3"
        android:background="#994"/>
</LinearLayout>
</LinearLayout>

enter image description here

相关问题