在RelativeLayout中居中SeekBar和ImageButton

时间:2014-01-30 13:54:51

标签: android

我有以下xml:

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

    <ImageButton
        android:id="@+id/reset"
        android:src="@drawable/ic_action_refresh"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="@string/reset" 
        android:onClick="reset"
        android:layout_alignParentRight="true" />

    <SeekBar
        android:id="@+id/seekbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_toLeftOf="@id/reset" />

</RelativeLayout>

我尝试使用gravity="center"layout_gravity="center"但它没有用。如何使RelativeLayout的内容居中?

2 个答案:

答案 0 :(得分:0)

您可以使用这三种中的一种进行相对布局:

  1. android:layout_centerHorizontal设置为true
  2. android:layout_centerVertical设置为true
  3. android:layout_centerInParent设置为true

答案 1 :(得分:0)

使用以下代码:

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

<ImageButton
    android:id="@+id/reset"
    android:src="@drawable/ic_action_refresh"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="@string/reset" 
    android:onClick="reset"
    android:layout_alignParentRight="true"
    android:layout_centerHorizontal="true" />

<SeekBar
    android:id="@+id/seekbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:layout_toLeftOf="@id/reset"
    android:layout_centerHorizontal="true"  />
</RelativeLayout>