【问题标题】:How to push included layout to bottom of screen?如何将包含的布局推送到屏幕底部?
【发布时间】:2014-01-08 05:59:30
【问题描述】:

我有一个名为 footer.xml 的布局,稍后将包含在多个布局中。 Footer.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/bottomMenu"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:orientation="horizontal"
    android:layout_alignParentBottom="true"
    android:background="@drawable/bg_gradation"
    android:weightSum="1" >
*** STUFF GOES HERE ***
</LinearLayout>

Footer.xml 然后将被包含在其他布局中,如下所示..

<?xml version="1.0" encoding="utf-8"?>
<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/backrepeat">

    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
    <LinearLayout
       android:id="@+id/content_frame"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical"
       android:layout_marginBottom="0dp"
       android:weightSum="1">
        <ListView
            android:id="@+id/chatListView"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:divider="#00000000"
            android:scrollbars="none"
            android:dividerHeight="1dp"
            android:layout_weight=".80"/>
        <LinearLayout
            android:id="@+id/chatBar"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight=".13"
            android:orientation="horizontal"
            android:background="#242424"
            android:weightSum="1">
        </LinearLayout>
    </LinearLayout>
    <include layout="@layout/footer"/>

    <!-- android:layout_gravity="start" tells DrawerLayout to treat
         this as a sliding drawer on the left side for left-to-right
         languages and on the right side for right-to-left languages.
         The drawer is given a fixed width in dp and extends the full height of
         the container. A solid background is used for contrast
         with the content view. -->
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

因为我希望footer.xml 位于底部屏幕,所以我使用android:layout_gravity="bottom",如果从footer.xml 的角度来看,它会正确呈现

但是,当我尝试从其他布局中查看它时,它会抛出错误:

java.lang.UnsupportedOperationException (Error Log does not show helpful information at all).

编辑 1 这是我能得到的最好的错误消息..(我无法复制粘贴日志文本)

编辑 2 如果我以这种方式包含我的布局:

<LinearLayout
    android:layout_weight="1"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <include layout="@layout/footer"
        android:id="@+id/footer"/>
</LinearLayout>

它在 Eclipse 编辑器中呈现我想要的方式。但是,当我尝试在 Android 上运行它时,出现以下错误:

java.lang.IllegalStateException: Child android.widget.LinearLayout{a72d4938 V.E..... ......ID 0,0-0,0 #7f0a005c app:id/bottomMenu} at index 1 does not have a valid layout_gravity - must be Gravity.LEFT, Gravity.RIGHT or Gravity.NO_GRAVITY

如果我将 footer.xml 编辑为:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/bottomMenu"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" <---- not android:layout_gravity="bottom"
    android:background="@drawable/bg_gradation"
    android:weightSum="1" >

我的页脚会从上到下填满整个屏幕。

谁能帮我解决这个问题?谢谢..

【问题讨论】:

    标签: android xml android-layout android-linearlayout


    【解决方案1】:

    将线性布局和页脚放在相对布局中

       <include
            android:id="@+id/footer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            layout="@layout/activity_main" />
    

    XML:

    <?xml version="1.0" encoding="utf-8"?>
    <!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/backrepeat">
    
        <!-- As the main content view, the view below consumes the entire
             space available using match_parent in both dimensions. -->
        <RelativeLayout android:layout_width="match_parent"
           android:layout_height="match_parent">
        <LinearLayout
           android:id="@+id/content_frame"
           android:layout_above="@+id/footer"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:orientation="vertical"
           android:layout_marginBottom="0dp"
           android:weightSum="1">
            <ListView
                android:id="@+id/chatListView"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:divider="#00000000"
                android:scrollbars="none"
                android:dividerHeight="1dp"
                android:layout_weight=".80"/>
            <LinearLayout
                android:id="@+id/chatBar"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight=".13"
                android:orientation="horizontal"
                android:background="#242424"
                android:weightSum="1">
            </LinearLayout>
        </LinearLayout>
        <include layout="@layout/footer"     android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"/>
        </RelativeLayout>
        <!-- android:layout_gravity="start" tells DrawerLayout to treat
             this as a sliding drawer on the left side for left-to-right
             languages and on the right side for right-to-left languages.
             The drawer is given a fixed width in dp and extends the full height of
             the container. A solid background is used for contrast
             with the content view. -->
        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            android:background="#111"/>
    </android.support.v4.widget.DrawerLayout>
    

    【讨论】:

    • 把代码不够用,你从其他地方遇到问题
    猜你喜欢
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    • 1970-01-01
    • 2011-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多