RelativeLayout的不规则行为

时间:2018-02-09 06:01:12

标签: android android-relativelayout

这是我创建的设计:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="wrap_content">

    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:elevation="@dimen/dp_10"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:elevation="@dimen/dp_5"
            android:weightSum="1"
            >
            <LinearLayout
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="0.85"
                android:orientation="vertical"
                android:background="@color/white"
                android:padding="@dimen/dp_10"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/black_13"
                    android:textSize="@dimen/tv16"
                    android:fontFamily="@font/roboto_medium"
                    android:text="Abhishek Mehta"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/clr_40"
                    android:textSize="@dimen/tv13"
                    android:fontFamily="@font/roboto_medium"
                    android:text="Abhishek Mehta"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/clr_40"
                    android:textSize="@dimen/tv11"
                    android:fontFamily="@font/roboto_regular"
                    android:text="Abhishek Mehta"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/clr_40"
                    android:textSize="@dimen/tv12"
                    android:fontFamily="@font/roboto_medium"
                    android:text="Abhishek Mehta"
                    android:drawableLeft="@drawable/time_icon"
                    android:drawablePadding="@dimen/dp_5"
                    />
            </LinearLayout>
            <LinearLayout
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="0.15"
                android:orientation="vertical"
                android:background="@color/clr_f3"
                android:gravity="center"
                >
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:id="@+id/call"
                    android:background="?attr/selectableItemBackground"
                    >
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"

                        android:src="@drawable/ic_call"

                        />
                </LinearLayout>
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/clr_d9d8e3"
                    android:layout_marginLeft="@dimen/dp_10"
                    android:layout_marginRight="@dimen/dp_10"
                    />
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:id="@+id/directions"
                    android:background="?attr/selectableItemBackground"
                    >
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"

                        android:src="@drawable/ic_directions"

                        />
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
        <LinearLayout

            android:background="@drawable/red_gradient_bg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:fontFamily="@font/montserrat_medium"
                android:textSize="@dimen/tv12"
                android:text="@string/release"
                android:textColor="@color/white"
                android:layout_weight="1"
                android:gravity="center"
                android:padding="@dimen/dp_10"
                android:background="?attr/selectableItemBackground"

                />
            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="@color/white"
                android:layout_marginTop="@dimen/dp_5"
                android:layout_marginBottom="@dimen/dp_5"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:fontFamily="@font/montserrat_medium"
                android:textSize="@dimen/tv12"
                android:text="@string/accept"
                android:textColor="@color/white"
                android:layout_weight="1"
                android:gravity="center"
                android:padding="@dimen/dp_10"
                android:background="?attr/selectableItemBackground"

                />
        </LinearLayout>
    </LinearLayout>
    <LinearLayout

        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/homeextension"
            android:textColor="@color/white"
            android:background="@color/clr_795CC2"
            android:padding="@dimen/dp_5"
            android:fontFamily="@font/roboto_bold"
            />
    </LinearLayout>
</RelativeLayout>

这是我得到的设计:

enter image description here

这是我想要的设计:

enter image description here

正如您所看到的,第二个线性布局落后于第一个线性布局,而不是相对布局的工作方式。第二个线性布局包含带有紫色背景的“Home Extension”文本。

2 个答案:

答案 0 :(得分:1)

RelativeLayout的不正常行为是因为android:elevation每当我们用任何视图声明这个属性时它会更改它的Z-Index以便视图出现在所有视图之上

为您的案例解决方案 1)从LinearLayout中删除android:elevation="@dimen/dp_10"

替换

<LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      android:elevation="@dimen/dp_10">

 <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical">

OR  在视图中添加相同的高程,以便将其引入前面

 <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:elevation="@dimen/dp_10">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:padding="@dimen/dp_5"
            android:text="@string/homeextension"
            android:textColor="@color/white" />
    </LinearLayout>

适用于RelativeLayoutFragmeLayout

的相同行为

答案 1 :(得分:0)

这是你的解决方案试试这个

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:elevation="@dimen/dp_10"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="@dimen/dp_5"
        android:weightSum="1">

        <LinearLayout
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="0.85"
            android:background="@color/white"
            android:orientation="vertical"
            android:padding="@dimen/dp_10"
            android:paddingLeft="10dp"
            android:paddingTop="12dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:fontFamily="@font/roboto_medium"
                android:text="Abhishek Mehta"
                android:textColor="@color/black_13"
                android:textSize="@dimen/tv16" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:fontFamily="@font/roboto_medium"
                android:text="Abhishek Mehta"
                android:textColor="@color/clr_40"
                android:textSize="@dimen/tv13" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:fontFamily="@font/roboto_regular"
                android:text="Abhishek Mehta"
                android:textColor="@color/clr_40"
                android:textSize="@dimen/tv11" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableLeft="@drawable/time_icon"
                android:drawablePadding="@dimen/dp_5"
                android:fontFamily="@font/roboto_medium"
                android:text="Abhishek Mehta"
                android:textColor="@color/clr_40"
                android:textSize="@dimen/tv12" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="0.15"
            android:background="@color/clr_f3"
            android:gravity="center"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/call"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="?attr/selectableItemBackground"
                android:gravity="center">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"

                    android:src="@drawable/ic_call"

                    />
            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginLeft="@dimen/dp_10"
                android:layout_marginRight="@dimen/dp_10"
                android:background="@color/clr_d9d8e3" />

            <LinearLayout
                android:id="@+id/directions"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="?attr/selectableItemBackground"
                android:gravity="center">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"

                    android:src="@drawable/ic_directions"

                    />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/red_gradient_bg">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="?attr/selectableItemBackground"
            android:fontFamily="@font/montserrat_medium"
            android:gravity="center"
            android:padding="@dimen/dp_10"
            android:text="@string/release"
            android:textColor="@color/white"
            android:textSize="@dimen/tv12"

            />

        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:layout_marginBottom="@dimen/dp_5"
            android:layout_marginTop="@dimen/dp_5"
            android:background="@color/white" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="?attr/selectableItemBackground"
            android:fontFamily="@font/montserrat_medium"
            android:gravity="center"
            android:padding="@dimen/dp_10"
            android:text="@string/accept"
            android:textColor="@color/white"
            android:textSize="@dimen/tv12"

            />
    </LinearLayout>
</LinearLayout>

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:fontFamily="@font/roboto_bold"
        android:padding="@dimen/dp_5"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="15dp"
        android:text="@string/homeextension"
        android:textColor="@color/white" />
</LinearLayout>