使用相对布局进行Android布局

时间:2013-02-20 09:02:53

标签: android layout relativelayout

有人可以解释一下为什么下面的代码段不能按预期工作吗? 我不能将android:layout_alignParentBottom="true"参数用于第一个文本视图,因为这样做会将视图放在整个屏幕的底部。

<?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="wrap_content" >

    <TextView 
        android:id="@+id/bottom_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/color_black"
        android:text="AT THE BOTTOM OF THE LAYOUT"/>


     <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/bottom_text_view"
        android:textColor="@color/color_black"
        android:text="ABOVE THE PREVIOUS TEXT VIEW"/>

</RelativeLayout>

Eclipse说@id/bottom_text_view无法解决。

请注意这个例子是一个简单的实例,我希望在按钮上放置更复杂的视图,即按照以下顺序放置可折叠列表视图和日历:

  • 的ListView
  • 日历
  • 按钮

我真的需要折叠列表视图和日历,并且一旦完成了仍然可见的应该移动以包装内容。

请帮助解决这个问题。

提前致谢。

编辑: 下面是我的真实案例布局 列表视图没有占用我想要的空间。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/magnet_bkg" >

    <TextView
        android:id="@+id/assets_header_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/view_padding"
        android:gravity="center"
        android:ellipsize="end"
        android:lines="1"
        android:maxLines="1"
        android:singleLine="true"
        android:text="@string/lbl_asset_manager_title"
        android:textColor="@color/color_white"
        android:textStyle="bold"
        android:textSize="@dimen/top_title_text_size"
        android:background="@drawable/dashboard_title_gradient_bg"
        android:visibility="visible"/>


    <!-- Use the linear layout in order to control the margins in 
    one place only -->
    <LinearLayout 
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_marginLeft="@dimen/assets_list_margin_side"
       android:layout_marginRight="@dimen/assets_list_margin_side"
       android:layout_marginTop="@dimen/assets_list_margin_topbottom"
       android:layout_marginBottom="@dimen/assets_list_margin_topbottom"
       android:orientation="vertical">

        <!-- The header with the asset information -->
        <include 
            layout="@layout/activity_magnet_enterprise_company_assets_list_item"/>

        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="@dimen/view_padding"
            android:textColor="@color/color_white"
            android:textSize="@dimen/sub_title_text_size"
            android:text="@string/lbl_report_filter_section_title"
            android:gravity="center"
            android:singleLine="true"
            android:ellipsize="end"
            android:background="@color/color_title_bar"/>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/create_report_filter_bottom_margin"
            android:gravity="bottom">

           <!-- The generate report Button -->
           <LinearLayout
                android:id="@+id/create_report_btn_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/filter_by_date_section_layout"
                android:background="@color/color_repoback_dark"
                android:gravity="center"
                android:padding="@dimen/view_padding">

                <Button
                    style="@style/NormalButton"
                    android:onClick="onBtnCreateReportClick"
                    android:text="@string/lbl_button_create_report" />
            </LinearLayout> <!-- end of generate report Button layout -->


            <!-- The filter by date section -->
            <LinearLayout
                android:id="@+id/filter_by_date_section_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/filter_by_project_section_layout"
                android:orientation="vertical">

                <!-- Sub header with the filter by date switch control -->

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/color_subtitle_bar"
                    android:padding="@dimen/view_padding_large" >

                    <Switch
                        android:id="@+id/filter_by_date_switch"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:layout_centerVertical="true" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:layout_toLeftOf="@id/filter_by_date_switch"
                        android:ellipsize="end"
                        android:gravity="left"
                        android:singleLine="true"
                        android:text="@string/lbl_report_filter_by_date"
                        android:textColor="@color/color_white"
                        android:textSize="@dimen/sub_title_text_size_small" />
                </RelativeLayout>

                <!-- The section for date selection -->
                <LinearLayout
                    android:id="@+id/date_selection_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/color_repoback_dark"
                    android:baselineAligned="false"
                    android:orientation="horizontal"
                    android:padding="@dimen/view_padding" >

                    <!-- Layout FROM date -->

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:orientation="vertical" >

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:gravity="center"
                            android:padding="@dimen/view_padding"
                            android:text="@string/lbl_report_date_from"
                            android:textColor="@color/color_white"
                            android:textSize="@dimen/small_text_size"
                            android:textStyle="bold" />

                        <DatePicker
                            android:id="@+id/from_date_report"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:calendarViewShown="false"
                            android:padding="@dimen/view_padding" />
                    </LinearLayout>

                    <!-- Layout TO date -->

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:orientation="vertical" >

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:gravity="center"
                            android:padding="@dimen/view_padding"
                            android:text="@string/lbl_report_date_to"
                            android:textColor="@color/color_white"
                            android:textSize="@dimen/small_text_size"
                            android:textStyle="bold" />

                        <DatePicker
                            android:id="@+id/to_date_report"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:calendarViewShown="false"
                            android:padding="@dimen/view_padding" />
                    </LinearLayout>
                </LinearLayout>
            </LinearLayout> <!-- end of filter by date section layout -->

            <!-- The filter by project section -->
            <LinearLayout
                android:id="@+id/filter_by_project_section_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <!-- Sub header with the filter by project switch control -->

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/color_subtitle_bar"
                    android:padding="@dimen/view_padding_large" >

                    <Switch
                        android:id="@+id/filter_by_project_switch"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:layout_centerVertical="true" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:layout_toLeftOf="@id/filter_by_project_switch"
                        android:ellipsize="end"
                        android:gravity="left"
                        android:singleLine="true"
                        android:text="@string/lbl_report_filter_by_project"
                        android:textColor="@color/color_white"
                        android:textSize="@dimen/sub_title_text_size_small" />
                </RelativeLayout>
                <!-- The projects list view -->

                <ListView
                    android:id="@+id/projects_listview"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/color_schedule_list_bg"
                    android:choiceMode="singleChoice"
                    android:divider="@color/color_lightgray"
                    android:dividerHeight="1dp" />
            </LinearLayout>

        </RelativeLayout>

    </LinearLayout>    

</LinearLayout>

3 个答案:

答案 0 :(得分:0)

试试这个

    <?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" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:text="AT THE BOTTOM OF THE LAYOUT" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ABOVE THE PREVIOUS TEXT VIEW" />

</RelativeLayout>

答案 1 :(得分:0)

因为您将height的{​​{1}}设置为RelativeLayout 在我改变它之后复制这段代码并看看它是如何工作的

wrap_content

答案 2 :(得分:0)

这是我刚创建的布局!

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="52dp"
        android:textSize="30sp"
        android:text="Hi I'm The Text On TOP" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="40dp"
        android:text="Hi I'm The Text Below the Text Above"
        android:textSize="30sp" />

</RelativeLayout>