2015-12-12 58 views
2

我在其下使用ScrollView和RelativeLayout。我在屏幕的底部有一個按鈕。 我希望按鈕在滾動時隨屏幕一起移動。按鈕應該出現在屏幕的右下角。屏幕上的修復按鈕

我已經嘗試使用FrameLayout如在與這個問題有關的答案之一所述,但我想我的按鈕在那裏在屏幕的右下角。 截至目前按鈕出現在整個屏幕的底部,這意味着您將不得不向下滾動一下才能看到它。

<ScrollView 
    android:layout_height="match_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:fillViewport="true" 
    > 
    <RelativeLayout 
     android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:layout_width="match_parent" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     tools:context="com.example.android.app1.SecondScreen"> 

    <TextView 
     android:text="Check all the Symptoms which apply - " 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="25sp" 
     android:id="@+id/textView" /> 

    <CheckBox1> 
    <CheckBox2> 
    <CheckBox3> 
    <CheckBox4> 
    <CheckBox5> 
    <CheckBox6> 
    <CheckBox7> 
    <CheckBox8> 
    <CheckBox9> 
    <CheckBox10> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="25sp" 
      android:text="submit" 
      android:id="@+id/submitbutton" 
      android:layout_below="@id/Lastcheckbox" 
      android:layout_alignParentRight="true" 
      /> 
    </RelativeLayout> 
</ScrollView> 

如何實現這一目標?

回答

1

您將需要一個浮動的操作按鈕:

<RelativeLayout 
... 
xmlns:app="http://schemas.android.com/apk/res-auto"> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/myFAB" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_plus_sign" 
     app:elevation="4dp" 
     ... /> 

...

在你的應用水平gradle這個

compile 'com.android.support:design:23.1.1' 

要獲得wh OLE工作項目

更新的Android工作室1.5

創建一個新的項目機智空白活動,它會自動爲你生成!

+0

我已經下載了最新的更新,但現在該怎麼做更新? –

+0

如果你的android studio不是1.4+,那麼去幫助 - >檢查更新。如果你有1.4+版本...那麼你都將設置爲由android studio生成的晶圓廠,只是創建一個新的空白項目將爲你做的東西 – johnrao07

+0

我有Android 1.2。點擊幫助 - >檢查更新是否要求下載更新。我已經下載了更新。我應該如何繼續下去? –

1

使用FloatingActionBar而不是普通的按鈕它總是在屏幕的右下方使用設計支持庫。

<android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@android:drawable/ic_dialog_email" /> 
+0

如何使用appcompat v7庫? –

+0

在這個鏈接上獲得一個掠奪http://developer.android.com/tools/support-library/setup.html –

+0

如果你只是將庫添加到你的依賴關係 –

0

您可以用按鈕

您的滾動型分離
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent"> 
<ScrollView 
    android:id="@+id/myScrollView" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:fillViewport="true" 
    android:layout_above="@+id/submitbutton"> 
    <RelativeLayout 
     android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:layout_width="match_parent" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     tools:context="com.example.android.app1.SecondScreen"> 

    <TextView 
     android:text="Check all the Symptoms which apply - " 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="25sp" 
     android:id="@+id/textView" /> 

    <CheckBox1/> 
    <CheckBox2/> 
    <CheckBox3/> 
    <CheckBox4/> 
    <CheckBox5/> 
    <CheckBox6/> 
    <CheckBox7/> 
    <CheckBox8/> 
    <CheckBox9/> 
    <CheckBox10/> 


    </RelativeLayout> 
</ScrollView> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="25sp" 
     android:text="submit" 
     android:id="@+id/submitbutton" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentBottom="true" /> 
</RelativeLayout> 
0

你可以使用一個FloatingActionButton像其他人說。 OR 你可以放置在LinearLayoutScrollView和放置該按鈕的ScrollView後,像這樣:

<LinearLayout ....> 
    <ScrollView 
    android:layout_width=".." 
    android:layout_height="0" 
    android:layout_weight="1"> 
     <RelativeLayout> 
     . 
     . 
     . 
     </RelativeLayout> 
    </ScrollView> 
    <Button android:layout_gravity = "right" ..... /> 
</LinearLayout> 
+0

您是否認爲使用LinearLayout將消除將按鈕放置在屏幕右側的選項? –

+0

你可以隨時做