【问题标题】:Android RelativeLayout position itemsAndroid RelativeLayout 位置项
【发布时间】:2013-10-04 22:54:21
【问题描述】:

下图是我想要达到的最终结果:

到目前为止,我的代码在将按钮居中对齐方面效果很好:

<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

【问题讨论】:

  • 根据这个布局的其余部分应该做什么,你真的不需要两个相对的布局。所以你根本没有得到文本,或者它没有居中?
  • @Sualkcin 它没有居中,如果我像帖子中所说的那样放置 textview,则图像按钮不会居中。

标签: android xml eclipse android-relativelayout


【解决方案1】:

添加属性

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>

【讨论】:

  • 查看我对@codeMagic 的回答。我尝试了您的解决方案(添加了 layout_below),得到了相同的结果。
  • @user2742861 也将 layout_gravity=centre 属性添加到图像按钮
  • 也不起作用。 ImageButtons 没有 Layout_gravity 属性。我当前的布局是我在主题中发布的第一张没有“TEXT HERE”的图片,我还没有找到解决方案。
  • @user2742861 尝试将android:layout_centerInParent="true" 添加到您的ImageButtons,如果这不起作用将它们包装在LinearLayout 中,就像上面一样(编辑答案)
【解决方案2】:

从这个内部的RelativeLayout 中取出您的TextView 并为其赋予属性,并在其中添加一个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>

【讨论】:

  • 有了你的解决方案,我的最终代码是:pastebin.com/hbJqs4q4 我得到这个输出:s24.postimg.org/tvi45r9n9/Sem_T_tulo.png 所以,ImagesButtons 得到了中间。
  • 将您的ImageButtons 包裹在LinearLayout 中并将android:layout_centerInParent="horizontal" 添加到LinearLayout
  • 你好。使用您的解决方案更糟糕..或者我没有得到您想说的。如果我使用 LinearLayout 并将这些 ImageButtons 放在该 LinearLayout 中,我将无法使用诸如 layout_toRightOf 等非常重要的属性,并且我的所有 ImageButtons 都以我想要的方式保持在中心之外(显示在第一张图像中) .
  • 查看我的编辑。类似的东西就是我的意思。如有必要,您可能需要稍微调整一些属性。但如果我明白你想要什么,那应该会让你接近。
  • 谢谢。我设法以这种方式工作:pastebin.com/9DwaQrPq,如果这是最好的方法,请告诉我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-14
  • 2015-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多