Android 菜单项的自定义视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26259162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom view for Menu Item
提问by Roman
I need to have dynamic Menu Item, a circle of user defined color, like this:
我需要动态菜单项,一个用户定义颜色的圆圈,如下所示:
touching this menu item will open a color picker.
触摸此菜单项将打开一个颜色选择器。
Now, I have sample ColorPickerIcon which extends View
现在,我有扩展 View 的示例 ColorPickerIcon
public class ColorPickerIcon extends View {
private Paint mPaint;
private int mColor;
private final int mRadius = 20;
public ColorPickerIcon(Context context) {
super(context);
mColor = Color.BLACK;
mPaint = createPaint();
}
public ColorPickerIcon(Context context, AttributeSet attrs) {
super(context, attrs);
mColor = Color.BLACK;
mPaint = createPaint();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(0, 0, mRadius, mPaint);
}
public void setPaintColor(int color) {
mColor = color;
}
private Paint createPaint() {
Paint temp = new Paint();
temp.setAntiAlias(true);
temp.setStyle(Paint.Style.STROKE);
temp.setStrokeJoin(Paint.Join.ROUND);
temp.setStrokeWidth(6f);
temp.setColor(mColor);
return temp;
}
}
and menu.xml
和 menu.xml
<item
android:id="@+id/menu_pick_color"
android:title="@string/pick_color"
yourapp:showAsAction="always"
yourapp:actionViewClass="com.example.widgets.ColorPickerIcon"/>
<item
android:id="@+id/menu_clear"
android:icon="@null"
android:title="@string/clear"
yourapp:showAsAction="always"/>
<item
android:id="@+id/menu_save"
android:icon="@null"
android:title="@string/save"
yourapp:showAsAction="always"/>
But it doesn't work this way, neither can I instantiate the class nor it's rendered. Is there a way to use custom class and custom dynamic view as Menu Item?
但它不是这样工作的,我也不能实例化这个类,也不能渲染它。有没有办法将自定义类和自定义动态视图用作菜单项?
采纳答案by Roman
Okay, so it turned out to be simpler than that.
好吧,事实证明它比那更简单。
In the DrawingActivity
在绘图活动中
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_drawing, menu);
MenuItem colorPicker = menu.findItem(R.id.menu_pick_color);
ShapeDrawable circle = new ShapeDrawable(new OvalShape());
circle.getPaint().setColor(Color.GREEN);
circle.setIntrinsicHeight(120);
circle.setIntrinsicWidth(120);
circle.setBounds(0, 0, 120, 120);
colorPicker.setIcon(circle);
return true;
}
in menu.xml
在 menu.xml 中
<item
android:id="@+id/menu_pick_color"
android:title="@string/pick_color"
yourapp:showAsAction="always"/>
That's all.
就这样。
回答by Carlos J
What you need to do is create a layout file with the view that you want for the item, the when you declare the item on the menu, assign the layout like this:
您需要做的是创建一个布局文件,其中包含您想要的项目视图,当您在菜单上声明项目时,分配这样的布局:
<item
android:id="@+id/menu_pick_color"
android:title="@string/pick_color"
app:showAsAction="always"
app:actionLayout="@layout/my_custom_item"/>
And that's it!
就是这样!
EDIT:
编辑:
To access the custom item and modify it's color at runtime you can do this.
要在运行时访问自定义项并修改其颜色,您可以执行此操作。
In your activity (or fragment) override the onPrepareOptionsMenu
(Assuming you already inflated your menu with 'onCreateOptionsMenu')
在您的活动(或片段)中覆盖onPrepareOptionsMenu
(假设您已经使用“onCreateOptionsMenu”扩充了您的菜单)
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
//Get a reference to your item by id
MenuItem item = menu.findItem(R.id.menu_pick_color);
//Here, you get access to the view of your item, in this case, the layout of the item has a FrameLayout as root view but you can change it to whatever you use
FrameLayout rootView = (FrameLayout)item.getActionView();
//Then you access to your control by finding it in the rootView
YourControlClass control = (YourControlClass) rootView.findViewById(R.id.control_id);
//And from here you can do whatever you want with your control
return true;
}