Android 浮动操作按钮屏幕忽略屏幕比例
Android Floating Action button screen ignore screen scale
我对 RelatieLayout 中的 FloatingActionButton 有疑问。我使用 DP 作为边距顶部和左侧。当我更改设备时,按钮不在它的位置上。你可以在屏幕上看到。第一个来自编辑器,第二个来自设备。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="3dp"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:paddingTop="3dp">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerViewFloor5"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="430dp"
android:layout_marginLeft="300dp"
app:backgroundTint="@color/primaryColor"
android:src="@drawable/ic_action_calendar"
app:fabSize="normal"
android:id="@+id/floorActionButton"/>
</RelativeLayout>
[
我也试过android:gravity="center|bottom"
但是没有任何反应。
试试这个...不要使用保证金设置 FloatingActionButton
头寸总是使用 gravity
.
按照 androidhive 中的教程进行操作。
https://www.androidhive.info/2015/12/android-material-design-floating-action-button/
我已经设置了 layoutgravity
但根据 parentlayout
properties
为 gravity
正确使用 gravity
。
如果您按照上面的教程进行操作,这将很容易,因为在我的例子中,我将 fab 放在 </android.support.design.widget.CoordinatorLayout>
中,并像教程中一样在其中包含布局。
跟着它会很容易只是不要使用 margin
设置它的 position
它会 break
在不同的屏幕上像你上面 error
。
<android.support.design.widget.FloatingActionButton
android:id="@+id/floorActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:backgroundTint="@color/primaryColor"
android:src="@drawable/ic_action_calendar"
app:fabSize="normal"/>
I also tried android:gravity="center|bottom" but nothing happens.
由于您使用的是 RelativeLayout
- gravity
将不起作用。而是使用 android:layout_alignParentBottom
和边距
<RelativeLayout
....
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="16dp"
app:backgroundTint="@color/primaryColor"
android:src="@drawable/ic_action_calendar"
app:fabSize="normal"
android:id="@+id/floorActionButton"/>
</RelativeLayout>
我对 RelatieLayout 中的 FloatingActionButton 有疑问。我使用 DP 作为边距顶部和左侧。当我更改设备时,按钮不在它的位置上。你可以在屏幕上看到。第一个来自编辑器,第二个来自设备。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="3dp"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:paddingTop="3dp">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerViewFloor5"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="430dp"
android:layout_marginLeft="300dp"
app:backgroundTint="@color/primaryColor"
android:src="@drawable/ic_action_calendar"
app:fabSize="normal"
android:id="@+id/floorActionButton"/>
</RelativeLayout>
[
我也试过android:gravity="center|bottom"
但是没有任何反应。
试试这个...不要使用保证金设置 FloatingActionButton
头寸总是使用 gravity
.
按照 androidhive 中的教程进行操作。
https://www.androidhive.info/2015/12/android-material-design-floating-action-button/
我已经设置了 layoutgravity
但根据 parentlayout
properties
为 gravity
正确使用 gravity
。
如果您按照上面的教程进行操作,这将很容易,因为在我的例子中,我将 fab 放在 </android.support.design.widget.CoordinatorLayout>
中,并像教程中一样在其中包含布局。
跟着它会很容易只是不要使用 margin
设置它的 position
它会 break
在不同的屏幕上像你上面 error
。
<android.support.design.widget.FloatingActionButton
android:id="@+id/floorActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:backgroundTint="@color/primaryColor"
android:src="@drawable/ic_action_calendar"
app:fabSize="normal"/>
I also tried android:gravity="center|bottom" but nothing happens.
由于您使用的是 RelativeLayout
- gravity
将不起作用。而是使用 android:layout_alignParentBottom
和边距
<RelativeLayout
....
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="16dp"
app:backgroundTint="@color/primaryColor"
android:src="@drawable/ic_action_calendar"
app:fabSize="normal"
android:id="@+id/floorActionButton"/>
</RelativeLayout>