我需要为切换按钮https://github.com/mikepenz/MaterialDrawer/blob/fde00e5573310384b90d1f7f92d2b45c2ed8a2ba/library/src/main/res/layout/material_drawer_item_toggle.xml#L58设置自定义样式,但按钮位于外部库中。我怎么能成功呢?
在我的主题中,我补充说:
<item name="android:buttonStyleToggle">@drawable/btn_toggle</item>
在btn_toggle.xml中我添加:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="@drawable/state_off" />
<item android:state_checked="true" android:drawable="@drawable/state_on" />
</selector>
但是toggleButton变得不可见了。
答案 0 :(得分:1)
在MaterialDrawer库中获取自定义行为的最干净的实现是创建自己的Custom DrawerItem。
快速浏览DividerDrawerItem
,这是项目内部更简单的DrawerItems
之一,如何做到这一点。
在您的情况下,您想要修改ToggleDrawerItem。只需复制代码并根据需要进行修改。
public class ToggleDrawerItem extends BasePrimaryDrawerItem<ToggleDrawerItem> {
private boolean toggleEnabled = true;
private boolean checked = false;
private OnCheckedChangeListener onCheckedChangeListener = null;
public ToggleDrawerItem withChecked(boolean checked) {
this.checked = checked;
return this;
}
public ToggleDrawerItem withToggleEnabled(boolean toggleEnabled) {
this.toggleEnabled = toggleEnabled;
return this;
}
public ToggleDrawerItem withOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener) {
this.onCheckedChangeListener = onCheckedChangeListener;
return this;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public boolean isToggleEnabled() {
return toggleEnabled;
}
public void setToggleEnabled(boolean toggleEnabled) {
this.toggleEnabled = toggleEnabled;
}
public OnCheckedChangeListener getOnCheckedChangeListener() {
return onCheckedChangeListener;
}
public void setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener) {
this.onCheckedChangeListener = onCheckedChangeListener;
}
@Override
public String getType() {
return "TOGGLE_ITEM";
}
@Override
@LayoutRes
public int getLayoutRes() {
return R.layout.material_drawer_item_toggle;
}
@Override
public void bindView(RecyclerView.ViewHolder holder) {
//get our viewHolder
final ViewHolder viewHolder = (ViewHolder) holder;
//bind the basic view parts
bindViewHelper((BaseViewHolder) holder);
if (!isSelectable()) {
viewHolder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (toggleEnabled) {
viewHolder.toggle.setChecked(!viewHolder.toggle.isChecked());
}
}
});
}
viewHolder.toggle.setChecked(checked);
viewHolder.toggle.setOnCheckedChangeListener(checkedChangeListener);
viewHolder.toggle.setEnabled(toggleEnabled);
//call the onPostBindView method to trigger post bind view actions (like the listener to modify the item if required)
onPostBindView(this, holder.itemView);
}
@Override
public ViewHolderFactory getFactory() {
return new ItemFactory();
}
public static class ItemFactory implements ViewHolderFactory<ViewHolder> {
public ViewHolder factory(View v) {
return new ViewHolder(v);
}
}
private static class ViewHolder extends BaseViewHolder {
private ToggleButton toggle;
private ViewHolder(View view) {
super(view);
this.toggle = (ToggleButton) view.findViewById(R.id.material_drawer_toggle);
}
}
private CompoundButton.OnCheckedChangeListener checkedChangeListener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checked = isChecked;
if (getOnCheckedChangeListener() != null) {
getOnCheckedChangeListener().onCheckedChanged(ToggleDrawerItem.this, buttonView, isChecked);
}
}
};
}
更高级但更简单的解决方案是向要修改的项目添加onPostBindView
侦听器,并使用对视图的引用来搜索material_drawer_toggle
(ToggleButton) view.findViewById(R.id.material_drawer_toggle);
这也允许您根据需要修改和设置此按钮的样式。