[英]How use the real Floating Action Button (FAB) Extended?
消除對重復的任何疑慮或想法:在Material Design上定義什么是“擴展”。
但大多數人都將“擴展”與“ 轉型類型:快速撥號 ”混為一談,難以找到解決方案。 喜歡這里
問題所以我期待的是如何使用文本和擴展大小設置FAB。
今天我的代碼是這樣的:
<android.support.design.widget.FloatingActionButton
android:id="@+id/maps_main_distance_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="@string/str_distance_btn"
app:elevation="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
但我的按鈕看起來像這樣:
沒有文字,沒有正確的格式。 我在Constraint Layout中使用它。
implementation 'com.google.android.material:material:1.1.0-alpha04'
在您的xml文件中:
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_margin="@dimen/fab_margin"
android:text="Create"
app:icon="@drawable/outline_home_24" />
您可以創建一個實用程序類,使用ConstraintLayout
將MaterialButton
設置為Extended FloatingActionButton
。 您首先需要在xml中聲明MaterialButton
的兩個狀態,然后使用TransitionManager
在它們之間進行動畫處理。
你可以在這里閱讀一篇關於它的中篇文章,與此同時,我會在這里添加一些相關的代碼。
折疊的FAB狀態:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout android:id="@+id/extend_fab_container"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_fab"
android:elevation="8dp">
<android.support.design.button.MaterialButton
android:id="@+id/fab"
style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
android:layout_width="56dp"
android:layout_height="56dp"
app:cornerRadius="56dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
擴展的FAB狀態:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout android:id="@+id/extend_fab_container"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="8dp"
android:background="@drawable/bg_fab">
<android.support.design.button.MaterialButton
android:id="@+id/fab"
style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cornerRadius="56dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
背景圖紙:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/colorAccent" />
<corners android:radius="56dp" />
</shape>
和相關的Java擴展代碼:
private void setExtended(boolean extended, boolean force) {
if (isAnimating || (extended && isExtended() && !force)) return;
ConstraintSet set = new ConstraintSet();
set.clone(container.getContext(), extended ? R.layout.fab_extended : R.layout.fab_collapsed);
TransitionManager.beginDelayedTransition(container, new AutoTransition()
.addListener(listener).setDuration(150));
if (extended) button.setText(currentText);
else button.setText("");
set.applyTo(container);
}
您可以使用此庫來執行此操作。 (我用另一種方式)但是我使用FancyButton在協調器布局中創建一個圓形且非常棒的按鈕,如下所示:
<mehdi.sakout.fancybuttons.FancyButton
android:id="@+id/actMain_btnCompare"
app:layout_behavior="@string/fab_transformation_sheet_behavior"
android:layout_width="125dp"
android:layout_height="38dp"
android:layout_gravity="bottom|center"
android:layout_margin="20dp"
android:elevation="3dp"
android:padding="2dp"
fancy:fb_borderColor="@color/white"
fancy:fb_borderWidth="3dp"
fancy:fb_defaultColor="@color/colorPrimaryDark"
fancy:fb_radius="20dp"
fancy:fb_text="مقایسه محصول"
fancy:fb_textColor="@color/white"
fancy:fb_textGravity="center"
fancy:fb_textSize="13sp" />
結果是:
那可以有圖標(參見FancyButton )。
與app:layout_behavior="@string/fab_transformation_sheet_behavior"
它的行為如下所示:
祝好運。
簡短的回答:將背景設置為帶圓角的矩形,而不是橢圓形。 然后將半徑的尺寸設置為按鈕高度的一半。
我是怎么做到的:
<android.support.design.widget.FloatingActionButton
android:id="@+id/maps_main_distance_btn"
android:layout_width="@dimen/floating_button_width"
android:layout_height="@dimen/floating_button_height"
android:layout_marginBottom="8dp"
android:background="@drawable/btn_background_expanded"
android:text="@string/str_distance_btn"
app:elevation="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
繪制/ btn_background_expanded.xml:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item>
<shape android:shape="rectangle">
<solid android:color="?android:colorAccent" />
<corners
android:radius="@dimen/floating_button_radius"
/>
</shape>
</item>
</ripple>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.