如何将我的浮动按钮对齐到 Android Studio 页面底部。

huangapple go评论107阅读模式
英文:

how can i align my floating button to the bottom of the page in android studio

问题

  [1]: https://i.stack.imgur.com/OvP4P.png

// 父视图 ScrollView
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"    
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/back4">

    // ScrollView 内的相对布局子视图
    <RelativeLayout  
        android:layout_width="match_parent"    
        android:layout_height="match_parent"
        android:layout_marginTop="40dp">

        // 相对布局内的网格视图,但不允许浮动按钮对齐底部
        <GridView
            android:id="@+id/gridview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:verticalSpacing="50dp">
        </GridView>

        // 浮动按钮添加到屏幕底部
        <com.google.android.material.floatingactionbutton.FloatingActionButton   
            android:id="@+id/addNew"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/gridview"
            android:layout_alignParentEnd="true"
            android:src="@drawable/addbutton"/>
    </RelativeLayout>
</ScrollView>
英文:

//Parent View

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:background=&quot;@drawable/back4&quot;&gt;

//Relative Layout Child of ScrollView

&lt;RelativeLayout  

        android:layout_width=&quot;match_parent&quot;    
        android:layout_height=&quot;match_parent&quot;
        android:layout_marginTop=&quot;40dp&quot;&gt;

//Grid View with Parent Relative Layout. Grid View not allowing floating button to align bottom

    &lt;GridView
            android:id=&quot;@+id/gridview&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;match_parent&quot;
            android:verticalSpacing=&quot;50dp&quot;&gt;

    &lt;/GridView&gt;

//Floating Button adding to Bottom of screen

    &lt;com.google.android.material.floatingactionbutton.FloatingActionButton   

            android:id=&quot;@+id/addNew&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_below=&quot;@+id/gridview&quot;
            android:layout_alignParentEnd=&quot;true&quot;
            android:src=&quot;@drawable/addbutton&quot;/&gt;

//End of Relative Layout

&lt;/RelativeLayout&gt;

//End of Scroll View

</ScrollView>

答案1

得分: 2

请对您的FloatingActionButton XML进行一些更改。希望这对您有所帮助!

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/addNew"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:layout_margin="16dp"
    android:src="@drawable/addbutton" />

另外,不需要使用垂直滚动的ScrollView,因为GridView也是一个垂直滚动的小部件(GridView)。

英文:

Make some changes to your FloatingActionButton XML. Hope this works for you!

&lt;com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id=&quot;@+id/addNew&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:layout_alignParentEnd=&quot;true&quot;
    android:layout_alignParentRight=&quot;true&quot;
    android:layout_alignParentBottom=&quot;true&quot;
    android:layout_margin=&quot;16dp&quot;
    android:src=&quot;@drawable/addbutton&quot; /&gt;

Also, there is no need of vertically scrolling ScrollView as GridView is also a vertically scrolling widget(GridView).

huangapple
  • 本文由 发表于 2020年5月30日 19:00:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/62101457.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定