菜单项的自定义视图

时间:2014-10-08 14:15:46

标签: android android-actionbar menuitem

我需要动态菜单项,用户定义颜色的圆圈,如下所示:

enter image description here

触摸此菜单项将打开颜色选择器。

现在,我有一个示例ColorPickerIcon,它扩展了View

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 个答案:

答案 0 :(得分:54)

您需要做的是创建一个布局文件,其中包含您想要的项目视图,当您在菜单上声明项目时,指定如下布局:

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

那就是它!

修改

要访问自定义项目并在运行时修改它的颜色,您可以执行此操作。

在您的活动(或片段)中覆盖onPrepareOptionsMenu(假设您已经使用&#39; onCreateOptionsMenu&#39;来夸大您的菜单)

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

答案 1 :(得分:13)

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

在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"/>

enter image description here

就是这样。