Android布局与相对布局

问题描述:

有人可以请解释为什么下面的代码片段不能按预期工作? 我不能在第一个文本视图中使用android:layout_alignParentBottom="true"参数,因为这样会将视图置于整个屏幕的底部。Android布局与相对布局

<?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控件
  • 日历
  • 按钮

,我真的很需要两个塌陷的ListView和日历,一旦做了什么仍然可见应以T移至o包装到内容。

请帮助这个东西。

在此先感谢。

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

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

试试这个

<?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> 
+0

他的问题是相对布局高度是wrap_content布局将位于顶部而不填充父项! – k0sh 2013-02-20 09:08:59

+0

这不适合我的情况,因为正如我所说的,真正的用例会让列表视图以这种方式获得所有可用空间。嘿,等一下也许这值得一试......我会让你知道的。 – MadBlack 2013-02-20 09:17:14

+0

我尝试了使用列表视图的解决方案,它占用了我怀疑的所有空间。我要编辑我的帖子,以提供我真正的布局,希望你能帮助。也许我做了一些错误 – MadBlack 2013-02-20 10:21:05

它,因为你设置的RelativeLayoutwrap_content 复制此代码height后,我改变了一下,看看它是如何工作

<?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/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> 
+0

谢谢你的回答,但结果并没有改变:( – MadBlack 2013-02-20 09:15:38

这里是我刚刚创建的布局!

<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> 
+0

再次感谢,但如果放在顶部的视图是一个空间贪婪的例如ListView,这将无法正常工作。 – MadBlack 2013-02-20 09:24:14