【问题标题】:display button in relativeout below relativelayout在相对布局下方的相对布局中显示按钮
【发布时间】:2014-03-20 14:44:28
【问题描述】:

我的问题是我无法在列表视图下的布局中显示两个按钮。下面我在相对布局中有一个列表视图。我有第二个相对布局,其中包含 2 个按钮。按钮似乎显示在列表顶部,而我希望它们位于列表下方。

我该怎么做? 我尝试在第二个 RRlayout 中设置“android:layout_below”,将第一个布局作为锚点。

提前致谢。

<?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/textviewcarername"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/blue_alpha_background"
        android:gravity="center"
        android:text="Rota "
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#FFFFFF" />

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/carerdetailsfragrellayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textviewcarername"
        android:background="@drawable/textviewbg"

        android:padding="10dp" >

        <ListView
            android:id="@+id/android:list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:cacheColorHint="#0000"
            android:divider="#700000ff"
            android:dividerHeight="4px" >
        </ListView>
    </RelativeLayout>

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"

         >

        <Button
            android:id="@+id/buttonprevious"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:text="Previous" />

        <Button
            android:id="@+id/buttonnext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="Next" />
    </RelativeLayout>

</RelativeLayout>

【问题讨论】:

    标签: android view android-relativelayout


    【解决方案1】:

    这个布局应该可以完成这项工作

    <?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"
         >
        <!-- Header -->
        <TextView
            android:id="@+id/textviewcarername"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/blue_alpha_background"
            android:gravity="center"
            android:text="Rota "
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#FFFFFF"
        />
        <!-- Bottom Layout (Footer) -->
        <RelativeLayout
            android:id="@+id/rlFooter"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
             >
            <Button
                android:id="@+id/buttonprevious"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:text="Previous"
            />
            <Button
                android:id="@+id/buttonnext"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="Next"
            />
        </RelativeLayout>
        <!-- ListView -->
        <RelativeLayout
            android:id="@+id/carerdetailsfragrellayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/textviewcarername"
            android:layout_above="@id/rlFooter"
            android:background="@drawable/textviewbg"
            android:padding="10dp"
            >
            <ListView
                android:id="@+id/android:list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:cacheColorHint="#0000"
                android:divider="#700000ff"
                android:dividerHeight="4px"
            />
        </RelativeLayout>
    </RelativeLayout>
    

    请注意,我将底部布局移到包含 ListView 的布局之前并为其分配了一个 id,因此它可以由 ListView 容器引用

    我还删除了额外的命名空间定义

    [编辑]

    我现在将 ListView 及其容器 wrap_content 更改为 ma​​tch_parent,以填充剩余空间。 ListView 现在应该非常适合(我忘了那个)

    [编辑 2]

    您可能希望删除包含 ListView 的 RelativeLayout 并将其属性添加到 ListView 本身,以帮助扁平化您的设计 - 以提高性能(尽可能少)。

    【讨论】:

    • 第二个RR布局中有2个layout_below
    • 哦!当然!第二个是 ABOVE ;) 现在要修复它 - 这是一个复制粘贴忘记错误
    • 嘿,谢谢,完美。我可以问一个问题吗?为什么我不能只在第二个 RL 中指定 android:layout_below="@id/carerdetailsfragrellayout"?
    • 我总是这样做:将其锚定在页眉下方和页脚上方,以增强稳定性和可读性。但它可能也可以在没有锚定的情况下工作。您可能希望删除包含 ListView 的 RelativeLayout 并将其属性添加到 ListView 本身,以帮助扁平化您的设计 - 以提高性能(尽可能少)。
    【解决方案2】:

    试试这个..

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/textviewcarername"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Rota "
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#000000" />
    
        <RelativeLayout
            android:id="@+id/carerdetailsfragrellayout"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:padding="10dp" >
    
            <ListView
                android:id="@+id/android:list"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:cacheColorHint="#0000"
                android:divider="#700000ff"
                android:dividerHeight="4px" >
            </ListView>
        </RelativeLayout>
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <Button
                android:id="@+id/buttonprevious"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:text="Previous" />
    
            <Button
                android:id="@+id/buttonnext"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="Next" />
        </RelativeLayout>
    
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      给包含ButtonsRelativeLayout 一个id 作为android:id="@+id/button_panel" 并在包含ListViewRelativeLayout 中添加属性android:layout_above="@+id/button_panel",如下所示...

      <TextView
          android:id="@+id/textviewcarername"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:background="@color/blue_alpha_background"
          android:gravity="center"
          android:text="Rota "
          android:textAppearance="?android:attr/textAppearanceLarge"
          android:textColor="#FFFFFF" />
      
      <RelativeLayout
          android:id="@+id/carerdetailsfragrellayout"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@id/textviewcarername"
          android:background="@drawable/textviewbg"
          android:layout_above="@+id/button_panel"
          android:padding="10dp" >
      
          <ListView
              android:id="@+id/android:list"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:cacheColorHint="#0000"
              android:divider="#700000ff"
              android:dividerHeight="4px" >
          </ListView>
      </RelativeLayout>
      
      <RelativeLayout
          android:id="@+id/button_panel"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true" >
      
          <Button
              android:id="@+id/buttonprevious"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentLeft="true"
              android:text="Previous" />
      
          <Button
              android:id="@+id/buttonnext"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentRight="true"
              android:text="Next" />
      </RelativeLayout>
      

      【讨论】:

        猜你喜欢
        • 2011-05-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-06
        • 1970-01-01
        相关资源
        最近更新 更多