如何创建"浮动按钮"在Android中?

时间:2015-07-29 23:12:57

标签: android layout

我有ScrollView的活动,包括一些内容,还有按钮。我想做我的按钮(@ + id / buttonSubmit)总是在屏幕的底部可见,独立滚动内容。有什么想法吗?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true">

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/cyan50">

        <LinearLayout xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="@dimen/activity_vertical_margin"
            android:layout_marginLeft="@dimen/activity_horizontal_margin"
            android:layout_marginRight="@dimen/activity_horizontal_margin"
            android:layout_marginTop="@dimen/activity_vertical_margin"
            android:orientation="vertical"
            >

            // scroll content here

            <Button
                android:id="@+id/buttonSubmit"
                style="@style/ButtonLight"
                android:layout_gravity="center"
                android:layout_marginTop="@dimen/default_medium_margin"
                android:text="@string/submit_changes"/>


        </LinearLayout>
    </ScrollView>
</LinearLayout>

3 个答案:

答案 0 :(得分:0)

将根布局更改为FrameLayout,并将按钮设置为与滚动视图相同的级别,如果要确保它位于底部,请添加android:layout_gravity="bottom|right"。 XML应该是这样的:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true">

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/cyan50">

        <LinearLayout xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="@dimen/activity_vertical_margin"
            android:layout_marginLeft="@dimen/activity_horizontal_margin"
            android:layout_marginRight="@dimen/activity_horizontal_margin"
            android:layout_marginTop="@dimen/activity_vertical_margin"
            android:orientation="vertical"
            tools:context="com.brut.truckers.ui.activity.ProfileActivity">

            <LinearLayout style="@style/LayoutTitle">

                <TextView
                    style="@style/TextViewDefault"
                    android:layout_weight="1"
                    android:text="@string/trucker_name" />

            </LinearLayout>

            <EditText
                android:id="@+id/editTextTruckerName"
                style="@style/EditTextDefault"
                android:layout_marginTop="@dimen/default_small_margin"
                android:hint="@string/tap_to_edit" />


            <LinearLayout
                style="@style/LayoutTitle"
                android:layout_marginTop="@dimen/default_big_margin">

                <TextView
                    style="@style/TextViewDefault"
                    android:layout_weight="1"
                    android:text="@string/trucking_company" />

            </LinearLayout>

            <EditText
                android:id="@+id/editTextTruckerCompany"
                style="@style/EditTextDefault"
                android:layout_marginTop="@dimen/default_small_margin"
                android:hint="@string/tap_to_edit" />

            <LinearLayout
                style="@style/LayoutTitle"
                android:layout_marginTop="@dimen/default_big_margin">

                <TextView
                    style="@style/TextViewDefault"
                    android:layout_weight="1"
                    android:text="@string/tax_jurisdiction" />

            </LinearLayout>

            <EditText
                android:id="@+id/editTextTaxJurisdiction"
                style="@style/EditTextDefault"
                android:layout_marginTop="@dimen/default_small_margin"
                android:hint="@string/tap_to_edit" />

            <LinearLayout
                style="@style/LayoutTitle"
                android:layout_marginTop="@dimen/default_big_margin">

                <TextView
                    style="@style/TextViewDefault"
                    android:layout_weight="1"
                    android:text="@string/tax_registration" />

            </LinearLayout>

            <EditText
                android:id="@+id/editTextTaxRegistration"
                style="@style/EditTextDefault"
                android:layout_marginTop="@dimen/default_small_margin"
                android:hint="@string/tap_to_edit" />

            <LinearLayout
                style="@style/LayoutTitle"
                android:layout_marginTop="@dimen/default_big_margin">

                <TextView
                    style="@style/TextViewDefault"
                    android:layout_weight="1"
                    android:text="@string/driver_license_no" />

            </LinearLayout>

            <EditText
                android:id="@+id/editTextDriverLicenseNo"
                style="@style/EditTextDefault"
                android:layout_marginTop="@dimen/default_small_margin"
                android:hint="@string/tap_to_edit" />


        </LinearLayout>
    </ScrollView>

    <Button
        android:id="@+id/buttonSubmit"
        style="@style/ButtonLight"
        android:layout_gravity="bottom|right"
        android:layout_marginTop="@dimen/default_medium_margin"
        android:text="@string/submit_changes"/>
</FrameLayout >

答案 1 :(得分:0)

您可以使用例如一个RelativeLayout或一个FrameLayout来做这个

答案 2 :(得分:0)

  1. 应为最外面的LinearLayout分配属性android:orientation="vertical"
  2. 应为
  3. ScrollView分配属性android:layout_gravity="fill"
  4. Button应移出内部LinearLayoutScrollView,并位于最外ScrollView结束标记之前的LinearLayout之后
  5. 应为
  6. Button分配属性android:layout_gravity="bottom"