菜单项的自定义视图

25

我需要一个动态菜单项,一个用户定义颜色的圆形,就像这样:

enter image description here

点击此菜单项将打开一个颜色选择器。

现在,我有一个扩展了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;

}

}

以及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"/>

但是这样做不起作用,我既无法实例化该类,也无法渲染它。有没有办法将自定义类和自定义动态视图用作菜单项?

2个回答

62

你需要做的是创建一个带有所需视图的布局文件,然后在菜单中声明该项时像这样分配布局:

<item
    android:id="@+id/menu_pick_color"
    android:title="@string/pick_color"
    app:showAsAction="always"
    app:actionLayout="@layout/my_custom_item"/>

就是这样了!

编辑:

要在运行时访问自定义项并修改其颜色,可以执行以下操作。

在您的活动(或片段)中覆盖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;
}

谢谢,Carlos!我刚刚找到了一个有点不同的解决方案,对我很有效。 - Roman
1
这个方法可行,但我还需要手动连接事件,像这样:https://dev59.com/V18e5IYBdhLWcg3wCGu_#26187926。谢谢! - JimJty
1
如果这段代码不起作用,请尝试将 "android:actionLayout" 更改为 "app:actionLayout"。 - Kai Wang
3
如果您需要OnClickListener,请访问这里:https://dev59.com/gGgu5IYBdhLWcg3wCCxK - Kai Wang

14

好的,事实证明这比那更简单。

在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;
}

在 menu.xml 文件中

<item
    android:id="@+id/menu_pick_color"
    android:title="@string/pick_color"
    yourapp:showAsAction="always"/>

在此处输入图像描述

就这些。


哦,我的天啊,谢谢!我浪费了一个多小时尝试从MenuItem获取View对象以修改其背景和文本颜色,但这是一个非常好的和干净的答案。再次感谢!:D - devrique
2
点击颜色选择器没有反应?如何处理颜色选择器上的点击事件?假设我有一个自定义颜色选择器布局,我认为这会解答这个问题。 - cegprakash

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接