将LinearLayout的位置更改为屏幕的页脚

时间:2017-03-01 11:09:46

标签: android xml

我是android的新手,我在将红色linearLayout作为页脚放入屏幕时遇到问题,这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android           =   "http://schemas.android.com/apk/res/android"
    android:id              =   "@+id/mainWidgetContainer"
    android:layout_width    =   "match_parent"
    android:layout_height   =   "match_parent"
    android:background      =   "@color/blue"
    android:orientation     =   "vertical"
    xmlns:app="http://schemas.android.com/apk/res/....." >

    <RelativeLayout
        android:id="@+id/topPanel"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="@color/white">

        <Button
                android:id="@+id/languageButton"
                android:layout_width="60dp"
                android:layout_height="27dp"
                android:layout_alignParentRight="true"
                android:layout_marginRight="3dp"
                android:layout_centerVertical=  "true"
                android:visibility          =   "gone" />
    </RelativeLayout>


   <!-- to add the footer here -->
    <LinearLayout
        android:id              =   "@+id/bottomPanel"
        android:layout_width    =   "match_parent"
        android:layout_height   =   "45dp"
        android:layout_gravity="bottom"
        android:gravity="bottom"
        android:background="@color/red"
        android:orientation="horizontal">


    </LinearLayout>

</LinearLayout>

这是我得到的结果:

enter image description here

所以请任何帮助,我如何在屏幕的页脚中创建红色LinearLayout,我使用了android:gravity="bottom"这不起作用

感谢您的帮助

更新: 这是我应用Sir Lovekush Vishwakarma解决方案时得到的: enter image description here

白条消失了

这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android           =   "http://schemas.android.com/apk/res/android"
    android:id              =   "@+id/mainWidgetContainer"
    android:layout_width    =   "match_parent"
    android:layout_height   =   "match_parent"
    android:background      =   "@color/blue"
    android:orientation     =   "vertical"
    xmlns:app="http://schemas.android.com/apk/res/com.ofss.fcdb.mobile.android.phone.aman.launcher" >

    <RelativeLayout
        android:id="@+id/topPanel"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_weight="1"
        android:background="@color/white">

        <Button
                android:id="@+id/languageButton"
                android:layout_width="60dp"
                android:layout_height="27dp"
                android:layout_alignParentRight="true"
                android:layout_marginRight="3dp"
                android:visibility="gone" />
    </RelativeLayout>


    <!--     to add the footer here -->
<LinearLayout
        android:id="@+id/bottomPanel"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:layout_weight="0"
        android:background="@color/red"
        android:gravity="bottom"
        android:orientation="horizontal"></LinearLayout>   

</LinearLayout>

2 个答案:

答案 0 :(得分:3)

像这样使用,我已将LinearLayout更改为RelativeLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/....."
    android:id="@+id/mainWidgetContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/blue"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/topPanel"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="@color/white">

        <Button
            android:id="@+id/languageButton"
            android:layout_width="60dp"
            android:layout_height="27dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="3dp"
            android:visibility="gone" />
    </RelativeLayout>


    <!-- to add the footer here -->
    <LinearLayout
        android:id="@+id/bottomPanel"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_gravity="bottom"
        android:background="@color/red"
        android:gravity="bottom"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">
    </LinearLayout>

</RelativeLayout>

if you want to do it only by LinearLayout than use this below code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainWidgetContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/topPanel"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_weight="1"
        android:background="@color/white">

        <Button
            android:id="@+id/languageButton"
            android:layout_width="60dp"
            android:layout_height="27dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="3dp"
            android:visibility="gone" />
    </RelativeLayout>


    <!-- to add the footer here -->
    <LinearLayout
        android:id="@+id/bottomPanel"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:layout_weight="0"
        android:background="@color/red"
        android:gravity="bottom"
        android:orientation="horizontal"></LinearLayout>

</LinearLayout>

答案 1 :(得分:1)

使用以下XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainWidgetContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/topPanel"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_alignParentTop="true"
        android:background="#FFFFFF">

        <Button
            android:id="@+id/languageButton"
            android:layout_width="60dp"
            android:layout_height="27dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="3dp"
            android:visibility="gone" />
    </RelativeLayout>

    <!-- to add the footer here -->
    <LinearLayout
        android:id="@+id/bottomPanel"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:background="#FF0000"
        android:gravity="bottom"
        android:orientation="horizontal">
    </LinearLayout>

</RelativeLayout>