【问题标题】:How to keep Android Button view in the bottom of the screen?如何将 Android Button 视图保持在屏幕底部?
【发布时间】:2015-04-14 17:42:24
【问题描述】:

我有一个主要包含 2 个组件的活动:滚动视图和按钮。 我想把按钮放在屏幕的底部,固定的。

我尝试了很多东西,但我无法将按钮位置固定在屏幕按钮上,因为随着滚动视图内容的增加,按钮被推到活动屏幕之外。

我想实现这样的目标:

谁能告诉我如何做到这一点?

我的 XML 文件是干净的(所有自定义视图都是以编程方式编写的,无法共享!):

<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" />

【问题讨论】:

标签: android android-activity view


【解决方案1】:

使用LinearLayout

<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>

【讨论】:

    【解决方案2】:

    使用RelativeLayout 托管您的视图并将Button 固定到布局的底部。

    【讨论】:

    • 是的,确实有效!但它有一个问题。滚动视图填充按钮后面的空间。如何将滚动视图的高度限制为按钮的高度?
    • 更改布局中的ScrollView,使其与父顶部对齐并位于按钮上方。
    【解决方案3】:

    你可以像这样使用 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>
    

    【讨论】:

      【解决方案4】:

      如果你使用相对布局试试这个

      <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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-15
        • 2016-07-23
        • 2019-05-02
        • 2011-01-24
        • 1970-01-01
        • 2011-07-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多