【问题标题】:How to put an element on the right of some rows in RelativeLayout?如何将元素放在RelativeLayout中某些行的右侧?
【发布时间】:2015-03-17 10:29:14
【问题描述】:

我为我的列表行创建了这个布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/press_event" >

    <LinearLayout
        android:id="@+id/viewNameM"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/orange_button_pressed" >

        <TextView
            android:id="@+id/nameUser"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:gravity="center_horizontal"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#f6911e"
            android:textStyle="bold" />
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/viewDetails1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/viewNameM"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp" 
        android:layout_marginRight="10dp" >

        <ImageView
            android:id="@+id/arrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/catUser"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/catUser"
            android:contentDescription="@string/arrowS"
            android:src="@drawable/arrow" />

        <TextView
            android:id="@+id/catUser"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginBottom="3dp"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#4a4c4d" />

        <RatingBar
            android:id="@+id/ratingUser"
            style="?android:attr/ratingBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/arrow"
            android:layout_marginRight="20dp"
            android:layout_marginBottom="3dp"
            android:layout_alignBottom="@+id/catUser"
            android:indeterminate="false"
            android:isIndicator="true"
            android:numStars="@integer/five"
            android:stepSize="0.1" />

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/viewDetails2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/viewDetails1"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp" 
        android:layout_marginRight="10dp" >

        <TextView
            android:id="@+id/cityUser"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:textStyle="bold"
            android:layout_marginBottom="3dp"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#4a4c4d" />

        <TextView
            android:id="@+id/distance"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </RelativeLayout>
</RelativeLayout>

这是输出:

我想将箭头保留在行的右侧,但我想将它设置在中间,如下所示:

我尝试了几次,但我无法达到我的目标。怎么做? 提前谢谢你。

【问题讨论】:

    标签: android xml alignment rows android-relativelayout


    【解决方案1】:

    就像在现实生活中那样做:

    1. 将插入符号放在右侧。
    2. 将评分放在插入符号左侧,分隔线下方。
    3. 将距离放置在插入符号左侧,低于评级。
    4. ...

    【讨论】:

      【解决方案2】:

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:background="#ffffff" >
      
          <LinearLayout
              android:id="@+id/viewNameM"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:background="#cccccc" >
      
              <TextView
                  android:id="@+id/nameUser"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_margin="10dp"
                  android:gravity="center_horizontal"
                  android:textAppearance="?android:attr/textAppearanceMedium"
                  android:textColor="#f6911e"
                  android:textStyle="bold" />
          </LinearLayout>
      
          <RelativeLayout
              android:id="@+id/viewDetails1"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_below="@+id/viewNameM"
              android:layout_marginTop="10dp"
              android:layout_marginLeft="10dp"
              android:layout_marginRight="10dp" >
      
              <ImageView
                  android:id="@+id/arrow"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignParentRight="true"
                  android:layout_centerVertical="true"
                  android:src="@drawable/chevron" />
      
              <TextView
                  android:id="@+id/catUser"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignParentLeft="true"
                  android:text="Hotel"
                  android:layout_marginBottom="3dp"
                  android:textAppearance="?android:attr/textAppearanceMedium"
                  android:textColor="#4a4c4d" />
      
              <RatingBar
                  android:id="@+id/ratingUser"
                  style="?android:attr/ratingBarStyleSmall"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_toLeftOf="@+id/arrow"
                  android:layout_marginBottom="3dp"
                  android:layout_alignBottom="@+id/catUser"
                  android:indeterminate="false"
                  android:isIndicator="true"
                  android:numStars="5"
                  android:stepSize="0.1" />
      
              <TextView
                  android:id="@+id/cityUser"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignParentLeft="true"
                  android:layout_below="@id/catUser"
                  android:textStyle="bold"
                  android:text="Roma"
                  android:layout_marginBottom="3dp"
                  android:textAppearance="?android:attr/textAppearanceMedium"
                  android:textColor="#4a4c4d" />
      
              <TextView
                  android:id="@+id/distance"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_toLeftOf="@+id/arrow"
                  android:layout_below="@id/ratingUser"
                  android:textColor="#4a4c4d"
                  android:text="22km"
                  android:textAppearance="?android:attr/textAppearanceMedium" />
      
      
          </RelativeLayout>
      
      
      </RelativeLayout>

      这看起来像你想要的。

      【讨论】:

      • 谢谢,它成功了...我只需将android:layout_marginLeft="10dp" 添加到箭头TextView。非常感谢您,我非常感谢您的回答。
      • 你不应该鼓励糟糕的代码。 LinearLayout没用,内部RelativeLayout也没用。
      • 我刚刚从问题中编辑了一个。很明显,它并不完美。
      【解决方案3】:

      使用 LinnerLayout 中的 Layout weight 创建这种类型的布局

      【讨论】:

      • 您的意思是创建另一个只包含插入符号的 RelativeLayout 吗?那么在哪里使用权重布局呢?
      • 不使用 Linnar-Layout。因为重量不适用于相对布局。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-07
      相关资源
      最近更新 更多