相对布局约束

时间:2019-11-13 12:03:32

标签: android android-relativelayout

我试图创建布局,但不了解如何将一个视图置于另一个视图下。 这是我的布局:

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom">

        <TextView
            android:id="@+id/scroll_to_continue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/cardview_dark_background"
            android:gravity="center"
            android:text="Scroll to continue with content"
            android:textColor="@color/colorAccent"
            android:textSize="18sp"
            android:typeface="sans" />


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_above="@id/scroll_to_continue">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/design_default_color_primary"
                android:gravity="center"
                android:text="Tap to Learn More"
                android:textColor="@color/colorAccent"
                android:textSize="18sp"
                android:typeface="sans" />
        </LinearLayout>

    </RelativeLayout>

</FrameLayout>

我想将LinearLayout放在ID为scroll_to_continue的TextView上方。有人可以帮我吗?

P.S。 android:layout_above="@id/scroll_to_continue"的运行效果不及addRule(RelativeLayout.ABOVE, scrollToContinue.getId());

2 个答案:

答案 0 :(得分:1)

首先,您需要在布局中提供TextView的位置。我已使用android:layout_alignParentBottom="true"将其设置为底部。然后就可以了。检查以下内容:

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom">

        <TextView
            android:id="@+id/scroll_to_continue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/cardview_dark_background"
            android:gravity="center"
            android:text="Scroll to continue with content"
            android:layout_alignParentBottom="true"
            android:textColor="@color/colorAccent"
            android:textSize="18sp"
            android:typeface="sans" />


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_above="@id/scroll_to_continue">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/design_default_color_primary"
                android:gravity="center"
                android:text="Tap to Learn More"
                android:textColor="@color/colorAccent"
                android:textSize="18sp"
                android:typeface="sans" />
        </LinearLayout>

    </RelativeLayout>

</FrameLayout>

答案 1 :(得分:1)

myDate.toString().substring(0,4);