簡體   English   中英

Android:BottomSheetMenuItem 不在菜單中顯示圖標

[英]Android: BottomSheetMenuItem doesn't show icon in Menu

我試圖在 BottomSheeet 中顯示圖標,但只顯示文本。 我在 SO 上閱讀了很多線程,但它們似乎都沒有幫助或陳舊。

尋求有關最新 android 版本的幫助。 感謝任何幫助。

List<MenuItem> bottomSheetMenuItems = new ArrayList<>(optionsList.size());
Drawable drawable = getResources().getDrawable(android.R.drawable.ic_dialog_email, 
getContext().getTheme());
MenuItem bottomSheetMenuItem = new BottomSheetMenuItem(
                getContext(),
                someId,
                "Test",
                drawable);

bottomSheetMenuItem.setChecked(true).setChecked(true);
bottomSheetMenuItems.add(bottomSheetMenuItem);

BottomSheet bottomSheet = new BottomSheet
            .Builder(getContext())
            .setTitle("Test Title")
            .setMenuItems(bottomSheetMenuItems)
            .create();

bottomSheet.show();

對於圖標,您必須為底部表創建一個 xml。

XML 示例

<LinearLayout app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
android:id="@+id/intervalBottomSheet"
android:layout_width="match_parent"
android:layout_height="400dp"
android:orientation="vertical"
android:background="?attr/colorPrimary"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

    <ImageView
        android:src="@drawable/ic_remove_red_eye_black_24dp" />

    <TextView
        android:text="MenuItem" />

Java代碼

final BottomSheetDialog mBottomSheetDialog = new BottomSheetDialog(getActivity());
View sheetView = getActivity().getLayoutInflater().inflate(R.layout.bottomsheet_interval, null);
mBottomSheetDialog.setContentView(sheetView);
mBottomSheetDialog.show();

如果您的菜單項是動態的而不是恆定的,您可以在底部表單對話框中使用 recyclerview。

考慮到這一點,您需要這樣做才能加載圖標

ImageView img= (ImageView) findViewById(R.id.image);
img.setImageResource(R.drawable.my_image);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM