如何使用包含相对布局的包含内容而不使用填充父级

时间:2016-10-18 10:35:56

标签: android relativelayout android-wrap-content

抱歉我的英语不好, 我设置RelativeLayout宽度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个孩子从右到左填充。请帮忙!..

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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>

答案 2 :(得分:-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>