相对布局底部对齐问题

时间:2013-12-22 18:37:39

标签: android android-layout

我正在尝试实现相对布局,类似于下图。 但是当尝试底部对齐右下方的文本视图时,它会离开屏幕。

在布局下设计的最佳/最小方式是什么?enter image description here

代码,目前我正在尝试:

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

    <RelativeLayout
        android:id="@+id/dash1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="top" >

        <RelativeLayout
            android:id="@+id/view_subdash_top_right"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_toRightOf="@id/tv_name" >

            <TextView
                android:id="@+id/tv_instrument_change"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/tv_text2"
                android:gravity="right"
                android:text="text1" />

            <TextView
                android:id="@+id/tv_text2"
                android:layout_width="match_parent"
            android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:gravity="right"
                android:text="text2" />
        </RelativeLayout>

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignBottom="@id/view_subdash_top_right"
            android:gravity="bottom|center"
            android:text="name" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/dash1"
        android:gravity="top" >

        <!-- Here to enter bottom left -->

        <TextView
            android:id="@+id/tv_bottom_right"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:gravity="right|bottom"
            android:text="bottom right" />
    </RelativeLayout>

</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

如果你想用这些视图填满整个屏幕,我会使用加权LinearLayout

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

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <View
                   android:layout_width="match_parent"
                   android:layout_height="match_parent"
                   android:layout_weight="4"/>

             <View
                   android:layout_width="match_parent"
                   android:layout_height="match_parent"
                   android:layout_weight="1"/>
    </LinearLayout>

    <LinearLayout
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_weight="1">

             <View
                   android:layout_width="match_parent"
                   android:layout_height="match_parent"
                   android:layout_weight="1"/>

             <View
                   android:layout_width="match_parent"
                   android:layout_height="match_parent"
                   android:layout_weight="4"/>
     </LinearLayout>
</LinearLayout>

答案 1 :(得分:1)

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

    <RelativeLayout
        android:id="@+id/rel_top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/tv_top_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@id/rel_top_right"
            android:text="Top Left Text" />
        <RelativeLayout
            android:id="@+id/rel_top_right"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:gravity="right"
            android:layout_toRightOf="@id/tv_top_left" >

            <TextView
                android:id="@+id/tv_top_text1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Top Text1" />

            <TextView
                android:id="@+id/tv_top_text2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/tv_top_text1"
                android:text="Top Text2" />
        </RelativeLayout>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/rel_top"
        android:gravity="top" >
        <TextView
            android:id="@+id/tv_bottom_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignBottom="@id/rel_bottom_left"
            android:text="Bottom Right Text" />

        <RelativeLayout
            android:id="@+id/rel_bottom_left"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@id/tv_bottom_right" >

            <TextView
                android:id="@+id/tv_bottom_text1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Bottom Text1" />
            <TextView
                android:id="@+id/tv_bottom_text2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/tv_bottom_text1"
                android:text="Bottom Text2" />
        </RelativeLayout>
    </RelativeLayout>
</RelativeLayout>