如何使用与菜单按钮上的复选框相同的波纹效果。即,波的颜色根据夹杂物而变化。 此刻,像这样:
菜单
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/elect"
android:icon="@drawable/ic_elect_off"
android:checkable="true"
android:checked="false"
android:title="@string/menu_elect"
app:showAsAction="ifRoom" />
</menu>
Java
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_rule, menu);
MenuItem item = menu.findItem(R.id.elect);
item.setChecked(rule.isElect());
item.setIcon(rule.isElect() ? R.drawable.ic_elect_on : R.drawable.ic_elect_off);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.elect) {
item.setChecked(!item.isChecked());
item.setIcon(item.isChecked() ? R.drawable.ic_elect_on : R.drawable.ic_elect_off);
rule.setElect(item.isChecked());
Labs.get(getActivity()).updateRule(rule);
}
return super.onOptionsItemSelected(item);
}
我试图这样做,但确实可行,但缺点是-按钮宽度的问题,很多代码。
菜单
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/elect"
app:actionViewClass="android.widget.CheckBox"
android:checkable="true"
android:title="@string/menu_elect"
app:showAsAction="ifRoom" />
</menu>
Java
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_rule, menu);
MenuItem item = menu.findItem(R.id.elect);
final CheckBox checkBox = (CheckBox) item.getActionView();
checkBox.setButtonDrawable(R.drawable.elect);
checkBox.setChecked(rule.isElect());
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
rule.setElect(checkBox.isChecked());
Labs.get(getActivity()).updateRule(rule);
}
});
}
选择电子绘图
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="false"
android:drawable="@drawable/ic_elect_off" />
<item
android:state_checked="true"
android:drawable="@drawable/ic_elect_on" />
</selector>
答案 0 :(得分:0)
menu / menu.xml
db.collection.aggregate([
{
$match: {}
},
{
$project: {
array: {
$map: {
input: "$array",
as: "arr",
in: {
a: "$$arr.a",
b: "$$arr.b",
c: {
$size: {
$filter: {
input: {
$objectToArray: {
$ifNull: [
"$$arr.c",
{}
]
}
},
cond: {
$eq: [
"$$this.v",
true
]
}
}
}
}
}
}
}
}
}
])
Java
<?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/elect"
android:icon="@drawable/ic_elect_off"
android:checkable="true"
android:checked="false"
android:title="@string/menu_elect"
app:actionViewClass="android.widget.CheckBox"
app:showAsAction="ifRoom" />
</menu>
layout / ic_elect.xml
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_rule, menu);
MenuItem item = menu.findItem(R.id.elect);
item.setActionView(R.layout.ic_elect);
item.getActionView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//TODO
//bonus
Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.rotation);
item.getActionView().startAnimation(animation);
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
drawable / ic_elect.xml
<?xml version="1.0" encoding="utf-8"?>
<CheckBox
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@null"
android:translationX="12dp"
android:button="@drawable/ic_elect" />
奖金 anim / rotation.xml
<?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/ic_elect_on" />
<item android:state_checked="false" android:drawable="@drawable/ic_elect_off" />
</selector>