[英]How do I use a floating action button as a square?
有大量的 SO 線程說他們的按鈕顯示為一個錯誤的正方形
帶有 Android 設計庫的方形 FloatingActionButton
但是我的目標是使用方形按鈕。 如何將circle floating action button
更改為方形按鈕?
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/standardBlue"
app:srcCompat="@drawable/ic_3d_model"/>
您可以使用shapeAppearanceOverlay
屬性使用 Material Components Library 中的官方FloatingActionButton
來實現。
只需使用:
<com.google.android.material.floatingactionbutton.FloatingActionButton
app:shapeAppearanceOverlay="@style/fab_square"
和
<style name="fab_square" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">0dp</item>
</style>
您最好使用 Robertlevonyan 的這個庫customFloatingActionButton 。
<com.robertlevonyan.examples.customfloatingactionbutton.view.FloatingLayout
android:id="@+id/floating_layout"
app:fabType="square"
android:layout_width="match_parent"
android:layout_height="match_parent">
FloatingActionButton不提供除rounded
以外的shapes
請參閱此請求:允許設置 FAB 的形狀
然而,除此之外還有很多方法可以做到,但我個人推薦的一種方法是使用robertlevonyan的robertlevonyan
庫,它易於使用且可自定義。
正方形
將以下代碼行添加到您的模塊(應用程序)級別 Gradle 文件
implementation 'com.robertlevonyan.view:CustomFloatingActionButton:3.0.1'
現在將以下代碼添加到activity_main.xml
<com.robertlevonyan.views.customfloatingactionbutton.FloatingActionButton
android:id="@+id/custom_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end" />
您也可以通過添加
app:fabType="square"
在 XML 中自定義它,但我更喜歡 MainActivity.java
然后在MainActivity.java
中自定義floatingactionbutton
public class MainActivity extends AppCompatActivity {
private FloatingActionButton floatingActionButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
floatingActionButton = findViewById(R.id.custom_fab);
floatingActionButton.setFabType(FabType.FAB_TYPE_SQUARE); //set button type to square
floatingActionButton.setFabIcon(getResources().getDrawable(R.drawable.ic_baseline_add_btn_24, null));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.