Android浮动按钮滚动视图

时间:2011-09-21 12:46:19

标签: android-layout

我必须在固定位置(右上角)放置一个按钮,这样当我滚动屏幕/视图时,按钮始终可见。它就像一个漂浮在视图上的按钮。该按钮始终显示在滚动视图上。

3 个答案:

答案 0 :(得分:4)

我做了一个虚拟应用程序,它的工作原理 这是布局

<?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"
    >
    <RelativeLayout android:layout_width="match_parent" android:id="@+id/relativeLayout1" android:layout_height="wrap_content">
        <ScrollView android:id="@+id/scrollView1" android:layout_height="fill_parent" android:layout_width="fill_parent">
            <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
                <RatingBar android:id="@+id/ratingBar1" android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content"></RatingBar>
                <Spinner android:layout_width="wrap_content" android:id="@+id/spinner1" android:layout_weight="1" android:layout_height="wrap_content"></Spinner>
                <Spinner android:layout_width="wrap_content" android:id="@+id/spinner2" android:layout_height="wrap_content" android:layout_weight="1"></Spinner>
                <SeekBar android:id="@+id/seekBar1" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="match_parent"></SeekBar>
                <QuickContactBadge android:layout_width="wrap_content" android:id="@+id/quickContactBadge1" android:layout_weight="1" android:layout_height="wrap_content"></QuickContactBadge>
                <RatingBar android:id="@+id/ratingBar2" android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content"></RatingBar>
                <RadioGroup android:layout_width="wrap_content" android:id="@+id/radioGroup1" android:layout_weight="1" android:layout_height="wrap_content">
                    <RadioButton android:text="RadioButton" android:layout_width="wrap_content" android:id="@+id/radio0" android:layout_height="wrap_content" android:checked="true"></RadioButton>
                    <RadioButton android:text="RadioButton" android:layout_width="wrap_content" android:id="@+id/radio1" android:layout_height="wrap_content"></RadioButton>
                    <RadioButton android:text="RadioButton" android:layout_width="wrap_content" android:id="@+id/radio2" android:layout_height="wrap_content"></RadioButton>
                </RadioGroup>
                <Spinner android:layout_width="match_parent" android:id="@+id/spinner3" android:layout_height="wrap_content" android:layout_weight="1"></Spinner>
                <TextView android:layout_width="wrap_content" android:id="@+id/textView1" android:layout_height="wrap_content" android:layout_weight="1" android:text="TextView" android:textAppearance="?android:attr/textAppearanceMedium"></TextView>
                <Button android:layout_width="wrap_content" android:text="Button" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/button2"></Button>
                <ProgressBar android:layout_width="wrap_content" android:layout_weight="1" android:id="@+id/progressBar1" android:layout_height="wrap_content" style="?android:attr/progressBarStyleHorizontal"></ProgressBar>
                <RatingBar android:id="@+id/ratingBar3" android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content"></RatingBar>
                <QuickContactBadge android:layout_width="wrap_content" android:id="@+id/quickContactBadge2" android:layout_weight="1" android:layout_height="wrap_content"></QuickContactBadge>
                <Button android:layout_width="wrap_content" android:text="Button" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/button3"></Button>
                <CheckBox android:layout_width="wrap_content" android:text="CheckBox" android:id="@+id/checkBox1" android:layout_height="wrap_content" android:layout_weight="1"></CheckBox>
            </LinearLayout>
        </ScrollView>
        <Button android:layout_height="wrap_content" android:text="Button" android:layout_width="wrap_content" android:id="@+id/button1"></Button>
    </RelativeLayout>
</LinearLayout>

答案 1 :(得分:2)

你应该有一个相对布局。在布局中你将有一个滚动视图来填充父级的高度和,并且按钮也将在布局内,但它将与父级顶部对齐为真......

这是答案,如果您有任何问题再次询问

答案 2 :(得分:0)

我猜您正在使用xml文件来定义布局。然后,您需要在ScrollView定义之外定义按钮,例如:

<ScrollView
    ...>

    ...
</ScrollView>

<Button
   ...>
</Button>
相关问题