Android RelativeLayout位置项

时间:2013-10-04 22:54:22

标签: android xml eclipse relativelayout

以下图片是我想要达到的最终结果: enter image description here

到目前为止,我的代码非常适合在中心对齐按钮:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center">

     <ImageButton
        android:id="@+id/button_A"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/my_border"
        android:layout_marginTop="25dp"
        android:src="@drawable/categories" />

    <ImageButton
        android:id="@+id/button_B"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/button_A"
        android:layout_marginLeft="34dp"
        android:layout_marginTop="25dp"
        android:background="@drawable/my_border"
        android:src="@drawable/shopping_cart" />


</RelativeLayout>

然后,在“button_B”之后,我试图在“TEXT HERE IN CENTER”中找到消息,但没有成功。

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

    <TextView 
        android:id="@+id/txt" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TEXT HERE IN CENTER"/>

 </RelativeLayout>

有什么想法吗?感谢。

编辑已解决。 http://pastebin.com/9DwaQrPq

2 个答案:

答案 0 :(得分:0)

添加属性

android:layout_centerInParent="true"
android:layout_below="id of thing you want it below"

到文本视图

也可以从内部Relative Layout

中取出文本视图

修改

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/itemRoot"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:clickable="false">

<ImageButton
    android:id="@+id/button_A"
    android:layout_centerInParent="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/my_border"
    android:layout_marginTop="25dp"
    android:src="@drawable/categories" />

<ImageButton
    android:id="@+id/button_B"
    android:layout_width="wrap_content"
    android:layout_centerInParent="true"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/button_A"
    android:layout_marginLeft="34dp"
    android:layout_marginTop="25dp"
    android:background="@drawable/my_border"
    android:src="@drawable/shopping_cart" />

</LinearLayout>

答案 1 :(得分:0)

TextView从此内RelativeLayout移出并为其提供属性,并为其添加gravity以使文字居中。

<TextView 
    android:id="@+id/txt" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:gravity="center"
    android:text="TEXT HERE IN CENTER"/>

如果这不起作用,请说明你得到了什么或者什么不起作用。此外,您目前所获得的图片会有所帮助。

修改

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"/>

 <ImageButton
    android:id="@+id/button_A"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/my_border"
    android:layout_marginTop="25dp"
    android:src="@drawable/categories" />

<ImageButton
    android:id="@+id/button_B"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/button_A"
    android:layout_marginLeft="34dp"
    android:layout_marginTop="25dp"
    android:background="@drawable/my_border"
    android:src="@drawable/shopping_cart" />
</LinearLayout>

<TextView 
    android:id="@+id/txt" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:gravity="center"
    android:text="TEXT HERE IN CENTER"/>
</RelativeLayout>