首页 > 开发 > Android > 正文

如何将view固定在屏幕底部?

2017-09-08 15:24:05  来源:网友分享

下面是我的layout代码:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent">    <TextView android:text="@string/welcome"        android:id="@+id/TextView"        android:layout_width="fill_parent"        android:layout_height="wrap_content">    </TextView>    <LinearLayout android:id="@+id/LinearLayout"        android:orientation="horizontal"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:gravity="bottom">            <EditText android:id="@+id/EditText"                android:layout_width="fill_parent"                android:layout_height="wrap_content">            </EditText>            <Button android:text="@string/label_submit_button"                android:id="@+id/Button"                android:layout_width="wrap_content"                android:layout_height="wrap_content">            </Button>    </LinearLayout></LinearLayout>

输入这样的代码会显示左下图的状态,但我想显示右下图的状态,最直接的方法是设置TextView中的height,但是这种设置无法让它可以左对齐,给button或者是entry留出足够的位置。我想让submit button和文本entry在底部保持固定的高度,然后可以正常浏览文本其余的部分。同样,在Linear Layout水平方向上,我想让submit按钮收起它的内容,然后让其它文本内容显示在屏幕上,

具体应该如何操作呢?各位最好能给出一些有关Relative Layouts的解决方法。

<?xml version="1.0" encoding="utf-8"?>    <RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent">    <TextView         android:text="@string/welcome"        android:id="@+id/TextView"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_alignParentTop="true">    </TextView>    <RelativeLayout         android:id="@+id/InnerRelativeLayout"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true" >        <Button             android:text="@string/label_submit_button"            android:id="@+id/Button"            android:layout_alignParentRight="true"            android:layout_width="wrap_content"            android:layout_height="wrap_content">        </Button>        <EditText             android:id="@+id/EditText"            android:layout_width="fill_parent"            android:layout_toLeftOf="@id/Button"            android:layout_height="wrap_content">        </EditText>    </RelativeLayout></RelativeLayout>

原问题:How to align views at the bottom of the screen?

解决方案

答:Janusz
(最佳答案)
我认为你可以试试RelativeLayout.,这样你可以用android:layout_alignParentBottom将button移动到屏幕底部。如果在Relative Layout中,底部没有显示相关内容,或许就是在它之前的layout占据了整个屏幕。如果出现这种情况,你可以最先在layout文件夹中设定view的位置,然后用android:layout_above将其它的内容设置在view之上。这样,底部的view可以占据更多的空间,layout中其它的内容也可以充分显示。


答:Bram Avontuur
在ScrollView中,这种方法不起作用,因为无论ScrollView是否置于底部,RelativeLayout都会重叠。所以,我选择FrameLayout来进行固定:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_height="fill_parent" android:layout_width="fill_parent"        android:fillViewport="true">    <LinearLayout android:id="@+id/LinearLayout01"        android:layout_width="fill_parent" android:layout_height="fill_parent"        xmlns:android="http://schemas.android.com/apk/res/android"        android:orientation="vertical">                <!-- content goes here -->                <!-- stretching frame layout, using layout_weight -->        <FrameLayout            android:layout_width="fill_parent" android:layout_height="fill_parent"            android:layout_weight="1">        </FrameLayout>                <!-- content fixated to the bottom of the screen -->        <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal">                        <!-- your bottom content -->        </LinearLayout>    </LinearLayout></ScrollView>

答:pseudosudo
你可以在Linear Layout中新建RelativeLayout:

<LinearLayout    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent">    <TextView android:text="welcome"        android:id="@+id/TextView"        android:layout_width="fill_parent"        android:layout_height="wrap_content">    </TextView>    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="match_parent">        <Button android:text="submit"            android:id="@+id/Button"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentBottom="true"            android:layout_alignParentRight="true">        </Button>        <EditText android:id="@+id/EditText"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_toLeftOf="@id/Button"            android:layout_alignParentBottom="true">        </EditText>    </RelativeLayout></LinearLayout>

答:Timores
Janusz的答案很正确,但是我觉得用RelativeLayouts并不是最佳选择,所以我介绍一种“filler”方法,将TextView设定为空白:

<!-- filler --><TextView android:layout_height="0dip"          android:layout_width="fill_parent"          android:layout_weight="1" />