如何在布局中将三个图像和文本向右移动?

时间:2017-11-17 07:41:36

标签: android android-layout android-linearlayout relativelayout

我有一个图标,左边是图片,后面是文字。

向右,三幅图像下面应该有三幅图像和三幅文字。我做了一切,但问题是我无法将三个图像和文字移向右侧。

虽然我给了LinearLayout(供参考:android:id =" @ + id / right_side_layout")android:gravity =" right | end"它没有走向右侧。

这是布局和截图。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_weight="1"
            android:src="@drawable/ic_mark" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_weight="1"
            android:src="@drawable/ic_profile" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:text="@string/route_plan_default_number"
                android:textColor="@color/black"
                android:textSize="@dimen/text_size_very_small" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:text="@string/route_plan_default_name"
                android:textColor="@color/dark_green"
                android:textSize="@dimen/text_size_very_small" />

        </LinearLayout>
  <!--
        This layout should be right side. Though I given  android:gravity="right|end" it's not moving right.
    -->
        <LinearLayout
            android:id="@+id/right_side_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="28dp"
            android:layout_weight="1"
            android:gravity="right|end"
            android:orientation="horizontal">

            <RelativeLayout
                android:id="@+id/relative"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <ImageView
                    android:id="@+id/sell_in_red"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_sell_in_red" />

                <TextView
                    android:id="@+id/textLabel"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/sell_in_red"
                    android:layout_centerInParent="true"
                    android:paddingTop="2dp"
                    android:text="In"
                    android:textSize="@dimen/text_size_very_small"
                    android:textStyle="bold" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="9dp"
                android:layout_marginRight="9dp">

                <ImageView
                    android:id="@+id/sell_in_yellow"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_in_yellow" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/sell_in_yellow"
                    android:layout_centerInParent="true"
                    android:paddingTop="2dp"
                    android:text="Out"
                    android:textSize="@dimen/text_size_very_small"
                    android:textStyle="bold" />
            </RelativeLayout>

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

                <ImageView
                    android:id="@+id/growth_icon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_sell_in_green" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/growth_icon"
                    android:layout_centerInParent="true"
                    android:gravity="center"
                    android:paddingTop="2dp"
                    android:text="growth"
                    android:textSize="@dimen/text_size_very_small"
                    android:textStyle="bold" />
            </RelativeLayout>
        </LinearLayout>


    </LinearLayout>

</LinearLayout>

您可以看到下面的图像,其中三个圆形图标没有向右侧移动。

enter image description here

2 个答案:

答案 0 :(得分:1)

将嵌套线性布局的宽度(从顶部开始的第2个)更改为match_parent。

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:orientation="horizontal">

答案 1 :(得分:0)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:text="route_plan_default_number"
                android:textColor="@color/black"
                android:textSize="12dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:text="route_plan_default_name"
                android:textColor="@color/black"
                android:textSize="18dp" />

        </LinearLayout>
        <LinearLayout
            android:id="@+id/right_side_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="28dp"
            android:layout_weight="1"
            android:gravity="right|end"
            android:weightSum="3"
            android:orientation="horizontal">

            <RelativeLayout
                android:id="@+id/relative"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content">

                <ImageView
                    android:id="@+id/sell_in_red"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:id="@+id/textLabel"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="In "
                    android:layout_centerHorizontal="true"
                    android:layout_below="@+id/sell_in_red"
                    android:textSize="15dp"
                    android:textStyle="bold" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="9dp"
                android:layout_weight="1"
                android:layout_marginRight="9dp">

                <ImageView
                    android:id="@+id/sell_in_yellow"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_below="@+id/sell_in_yellow"
                    android:paddingTop="2dp"
                    android:text="Out"
                    android:textSize="15dp"
                    android:textStyle="bold" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content">

                <ImageView
                    android:id="@+id/growth_icon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_below="@+id/growth_icon"
                    android:gravity="center"
                    android:paddingTop="2dp"
                    android:text="growth"
                    android:textSize="15dp"
                    android:textStyle="bold" />
            </RelativeLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>