2014-10-08 51 views
16

自定義視圖,我需要有動態的菜單項,用戶自定義顏色的圓圈,像這樣:的菜單項

enter image description here

觸摸該菜單項打開一個顏色選擇器。現在

,我有延伸查看樣品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"/> 

但它不以這種方式工作,既可以我實例化這個類,也沒有渲染它。有沒有辦法使用自定義類和自定義動態視圖作爲菜單項?

回答

11

好了,所以它原來是比簡單。

在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

這就是全部。

+0

哦,我的天啊,謝謝你!我浪費了一個小時或更長時間試圖從MenuItem中獲取View對象來修改它的背景和文本顏色,但這是一個非常好的和乾淨的答案。再次感謝! :D – devrique 2017-01-28 03:42:06

+2

單擊顏色選擇器時沒有任何操作?如何處理顏色選擇器上的點擊?假設我有一個顏色選擇器的自定義佈局。我想這會回答這個問題。 – cegprakash 2017-02-02 11:18:11

44

你需要做的是創建要用於項目的圖的佈局文件中,當你聲明菜單上的項目,分配這樣的佈局:

<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; 
} 
+0

然後我將如何動態更新顏色呢? – Roman 2014-10-08 14:32:45

+0

我剛編輯我的回答 – 2014-10-08 14:44:47

+0

謝謝,卡洛斯!我剛剛找到了一些不同的解決方案,這對我有用。 – Roman 2014-10-08 14:45:27