簡體   English   中英

如何在具有自定義背景的工具欄中添加復選框-Android

[英]How to Add a CheckBox in Toolbar with custom background - android

嗨,我是android的新手,在這里我想在工具欄上有一個帶有自定義背景的復選框,我的用例是:我想通過工具欄上的復選框將當前帖子(在我的活動中)添加到收藏夾列表中,我想使用星形(開/關)圖標。

我嘗試了這個但是checkBox為null

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/shwo_menu_download_mp3"
    android:title="@string/download_mp3"
    app:showAsAction="never"/>
<item android:id="@+id/show_menu_add_to_fav"
    android:checked="true"
    android:enabled="true"
    app:showAsAction="always"
    />
<item android:id="@+id/show_menu_setting"
    android:title="@string/show_menu_setting"
    app:showAsAction="never" />

和我的活動:

 @Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_speech);
    initCheckBox();
    ...
}

CheckBox checkBoxFav;

private void initCheckBox() {
    checkBoxFav = (CheckBox) findViewById(R.id.show_menu_add_to_fav);

    checkBoxFav.setText("some Text");
}

使用工具欄,您可以執行此操作

setSupportActionBar(toolbar);
View view= getLayoutInflater().inflate(R.layout.view_, null);
Checkbox chbox = view.findViewById(..);
//chbox.do what u want with it
toolbar.addView(logo);

視圖布局包含一個復選框,並根據需要進行設計

您必須按照以下步驟操作:
1.創建一個工具欄並將其添加到您的活動中。 我不會解釋這一點,因為有很多關於此問題的教程。
2.創建一個帶有復選框小部件的菜單:

<?xml version="1.0" encoding="utf-8"?>
<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >
    <item
        android:id="@+id/menu_star"
        android:checkable="true"
        app:actionViewClass="android.widget.CheckBox"
        app:showAsAction="ifRoom"
        android:title="@string/favorite" />

</menu>
  1. 將圖標集添加到drawable文件夾中。 一個圖標表示未選中狀態,另一個圖標表示選中狀態。

  2. 創建一個資源文件並將其命名為任意名稱(在此示例中,名稱為star.xml ),將其添加到drawable文件夾中,並在其中插入以下代碼:

     <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:drawable="@drawable/star_black" /> <item android:state_selected="true" android:drawable="@drawable/star_black" /> <item android:state_checked="false" android:drawable="@drawable/star_border_black" /> <item android:drawable="@drawable/star_border_black" /> </selector> 
  3. 在您的活動中創建一個onCreateOptionsMenu方法,並插入以下代碼。 選中或取消選中后,復選框的圖標將自動更改。

     @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater menuInflater = getMenuInflater(); menuInflater.inflate(R.menu.menu,menu); CheckBox checkBox = (CheckBox)menu.findItem(R.id.menu_star).getActionView(); checkBox.setButtonDrawable(R.drawable.star);//set the icon to star.xml checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { //set the action of the checkbox } }); return super.onCreateOptionsMenu(menu); } 

暫無
暫無

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

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