如何在屏幕底部保留Android按钮视图?
我有一个活动,它主要包含两个组件:一个滚动视图和一个按钮。 我想把按钮放在屏幕的按钮上,固定好了 我尝试了很多方法,但我无法将按钮位置固定在屏幕的按钮上,因为随着滚动视图内容的增加,按钮会被推到活动屏幕之外 我想实现这样的目标: 有人能告诉我如何做到这一点吗 我的XML文件是干净的(所有自定义视图都是以编程方式编写的,无法共享!):如何在屏幕底部保留Android按钮视图?,android,android-activity,view,Android,Android Activity,View,我有一个活动,它主要包含两个组件:一个滚动视图和一个按钮。 我想把按钮放在屏幕的按钮上,固定好了 我尝试了很多方法,但我无法将按钮位置固定在屏幕的按钮上,因为随着滚动视图内容的增加,按钮会被推到活动屏幕之外 我想实现这样的目标: 有人能告诉我如何做到这一点吗 我的XML文件是干净的(所有自定义视图都是以编程方式编写的,无法共享!): 使用相对布局来承载视图,并将按钮固定到布局底部。使用线性布局: <LinearLayout xmlns:android="http://schemas
使用
相对布局来承载视图,并将按钮固定到布局底部。使用线性布局
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:fillViewport="true">
<!-- ... -->
</ScrollView>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save Changes"/>
</LinearLayout>
如果使用相对布局,请尝试以下操作
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.testproject.MainActivity" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_above="@+id/button1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Button" />
</RelativeLayout>
您可以像这样使用LinearLayout或relativeLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/scrollView"
android:layout_gravity="center_horizontal">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editableViewGroup"></LinearLayout>
</ScrollView>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Save Changes"
android:id="@+id/customButton" />
</LinearLayout>
你能发布你的xml文件吗?嘿@ikis看这里:是的,这很有效!但它有一个问题。滚动视图将填充按钮后面的空间。如何将滚动视图的高度限制为按钮的高度?更改布局中的滚动视图
,使其与父顶部对齐,并位于按钮上方。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/scrollView"
android:layout_gravity="center_horizontal">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editableViewGroup"></LinearLayout>
</ScrollView>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Save Changes"
android:id="@+id/customButton" />
</LinearLayout>