2011-09-21 67 views
1

我必須在固定位置(右上角)放置一個按鈕,這樣當我滾動屏幕/視圖時,該按鈕始終可見。它就像一個漂浮在視圖上的按鈕。該按鈕在滾動視圖中始終可見。滾動視圖上的Android浮動按鈕

回答

5

我做了一個假的應用程序和它的作品 這裏是佈局

<?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> 
+0

非常感謝Lukap ....那就像我的魅力...... !! :) :) –

0

我想你正在使用XML文件來定義你的佈局。然後您需要定義ScrollView定義之外的按鈕,例如:

<ScrollView 
    ...> 

    ... 
</ScrollView> 

<Button 
    ...> 
</Button> 
2

您應該有一個相對佈局。在佈局中,你將有一個滾動視圖來填充父母的身高和與按鈕也將在佈局內,但它將與父母的頂部對齊到真正的...

這是答案,如果你有什麼問題再問

+0

嗨盧卡普,感謝您的答覆。該按鈕實際上浮動在滾動視圖上,即它與頂部位置的視圖重疊。如果按照您的說法行事,滾動將發生在按鈕下方,我希望按鈕在滾動視圖中始終可見。 –

+0

好吧,你沒有完全按照我所說的做:),把按鈕和滾動視圖放在相對佈局裏面,定義對齊父按鈕和按鈕填充父窗口 – Lukap

+0

@Lukap這個工程,證實。 – Cesarsk

0

傢伙,我知道這是一箇舊的文章僅供參考,谷歌已經想出了一個庫,用於支持很多像浮動按鈕等很酷的東西。值得一看。 Google Design Support Lib