-1

对不起我的英语不好,我设置了RelativeLayoutwidth wrap_content,但是它fill_parent

这是我的代码:

<RelativeLayout
    android:id="@+id/btnAddToBasket"
    android:layout_width="wrap_content"
    android:layout_height="@dimen/productDetailsAddToBasketHeight"
    android:layout_alignParentRight="true"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/green_add_to_basket_button"
    android:clickable="true"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imgBasketIcon"
        android:layout_width="@dimen/productDetailsAddToBasketHeight"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:background="@drawable/green_add_to_basket_icon"
        android:padding="5dip"
        android:layout_alignParentRight="tr"
        android:scaleType="fitCenter"
        android:src="@drawable/basket_reverse_icon" />

    <ImageView
        android:id="@+id/imgHalfCircle"
        android:layout_width="10dip"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="2dip"
        android:layout_toLeftOf="@id/imgBasketIcon"
        android:scaleType="fitXY"
        android:src="@drawable/add_to_basket_half_black" />

        <TextView
            android:id="@+id/lblAddToBasket"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="5dip"
            android:layout_marginRight="4dip"
            android:layout_toLeftOf="@id/imgHalfCircle"
            android:gravity="center"
            android:text="Add to Basket"
            android:textColor="@color/white"
            android:textSize="@dimen/SmallTextSize"
            android:textStyle="bold" />
</RelativeLayout>

我可以用LinearLayout这个吗?我希望我的RelativeLayout孩子从右到左填写。请帮忙!..

4

3 回答 3

0

使用LinearLayout而不是RelativeLayout如下: -

<LinearLayout
        android:id="@+id/btnAddToBasket"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/productDetailsAddToBasketHeight"
        android:layout_alignParentRight="true"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/green_add_to_basket_button"
        android:clickable="true"
        android:orientation="horizontal">


        <TextView
            android:id="@+id/lblAddToBasket"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dip"
            android:layout_marginRight="4dip"
            android:gravity="center"
            android:text="Add to Basket"
            android:textColor="@android:color/white"
            android:textSize="@dimen/SmallTextSize"
            android:textStyle="bold" />
        <ImageView
            android:id="@+id/imgHalfCircle"
            android:layout_width="10dip"
            android:layout_height="match_parent"
            android:layout_marginBottom="2dip"
            android:scaleType="fitXY"
            android:src="@drawable/add_to_basket_half_black" />
        <ImageView
            android:id="@+id/imgBasketIcon"
            android:layout_width="@dimen/productDetailsAddToBasketHeight"
            android:layout_height="match_parent"
            android:background="@drawable/green_add_to_basket_icon"
            android:padding="5dip"
            android:scaleType="fitCenter"
            android:src="@drawable/basket_reverse_icon" />
    </LinearLayout>

或者如果你想使用相对布局参考下面的 XML

<RelativeLayout
        android:id="@+id/btnAddToBasket"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/productDetailsAddToBasketHeight"
        android:layout_alignParentRight="true"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/green_add_to_basket_button"
        android:clickable="true"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/imgBasketIcon"
            android:layout_width="@dimen/productDetailsAddToBasketHeight"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:background="@drawable/green_add_to_basket_icon"
            android:padding="5dip"
            android:scaleType="fitCenter"
            android:layout_toRightOf="@+id/imgHalfCircle"
            android:src="@drawable/basket_reverse_icon" />

        <ImageView
            android:id="@+id/imgHalfCircle"
            android:layout_width="10dip"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_marginBottom="2dip"
            android:layout_toRightOf="@+id/lblAddToBasket"
            android:scaleType="fitXY"
            android:src="@drawable/add_to_basket_half_black" />

        <TextView
            android:id="@+id/lblAddToBasket"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="5dip"
            android:layout_marginRight="4dip"
            android:gravity="center"
            android:text="Add to Basket"
            android:textColor="@android:color/white"
            android:textSize="@dimen/SmallTextSize"
            android:textStyle="bold" />
    </RelativeLayout>
于 2016-10-18T10:57:16.407 回答
0

尝试这个

<RelativeLayout                                android:id="@+id/btnAddToBasket"
                            android:layout_width="wrap_content"
                            android:layout_height="@dimen/productDetailsAddToBasketHeight"
                            android:layout_alignParentRight="true"
                            android:layout_gravity="center_horizontal"
                            android:background="@drawable/green_add_to_basket_button"
                            android:clickable="true"
                            android:orientation="horizontal" >

                            <ImageView
                                android:id="@+id/imgBasketIcon"
                                android:layout_width="wrap_content"
                                android:layout_height="match_parent"
                                android:layout_alignParentTop="true"
                                android:background="@drawable/green_add_to_basket_icon"
                                android:padding="5dip"
                                android:layout_alignParentRight="tr"
                                android:scaleType="fitCenter"
                                android:src="@drawable/basket_reverse_icon" />

                            <ImageView
                                android:id="@+id/imgHalfCircle"
                                android:layout_width="10dip"
                                android:layout_height="match_parent"
                                android:layout_alignParentTop="true"
                                android:layout_marginBottom="2dip"
                                android:layout_toLeftOf="@id/imgBasketIcon"
                                android:scaleType="fitXY"
                                android:src="@drawable/add_to_basket_half_black" />

                            <TextView
                                android:id="@+id/lblAddToBasket"
                                android:layout_width="wrap_content"
                                android:layout_height="match_parent"
                                android:layout_alignParentTop="true"
                                android:layout_marginLeft="5dip"
                                android:layout_marginRight="4dip"
                                android:layout_toLeftOf="@id/imgHalfCircle"
                                android:gravity="center"
                                android:text="Add to Basket"
                                android:textColor="@color/white"
                                android:textSize="@dimen/SmallTextSize"
                                android:textStyle="bold" />
                        </RelativeLayout>
于 2016-10-18T10:38:39.280 回答
-1

请试试这个。分享以改进。

<RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
<LinearLayout android:id="@+id/btnAddToBasket"
    android:layout_width="match_parent"
    android:layout_height="100dp"
     android:gravity="right"
    android:orientation="horizontal"
    android:layout_alignParentRight="true"
    android:layoutDirection="rtl"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ImageView
        android:id="@+id/imgBasketIcon"
        android:layout_width="30dp"
        android:layout_height="match_parent"
        android:background="@drawable/email_line"
        android:padding="5dip"
        android:layout_gravity="right"
        android:scaleType="fitCenter"
         />

    <ImageView
        android:id="@+id/imgHalfCircle"
        android:layout_width="30dp"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:scaleType="fitXY"
        android:layout_gravity="end"
        android:src="@drawable/logo" />
    <ImageView
        android:id="@+id/imgHalfCircl"
        android:layout_width="30dp"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:src="@drawable/email_line" />
    <ImageView
        android:id="@+id/imgHalfCirl"
        android:layout_width="30dp"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:src="@drawable/logo" />
    <ImageView
        android:id="@+id/imgHalfCrcl"
        android:layout_width="30dp"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:src="@drawable/email_line" />
    <ImageView
        android:id="@+id/imgHafCirl"
        android:layout_width="30dp"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:src="@drawable/logo" />
    <ImageView
        android:id="@+id/imgHafCrl"
        android:layout_width="30dp"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:src="@drawable/logo" />
  </LinearLayout>
</RelativeLayout>
于 2016-10-18T11:47:59.527 回答