目前,我正在改变对我的看法动作条
菜单项
像这样:
I'm currently changing the view on my ActionBar
MenuItem
like so:
MenuItem menuItem = menu.add(title);
TextView tv = new TextView(context);
tv.setTypeface(myTypeface);
tv.setText("hello");
menuItem.setActionView(tv);
然而,按钮,我假设这个禁用该功能,因为我已经改变了而ActionView
。有反正我能做到这一点,同时保留了按键功能?
However this disabled the function of the button, which I'm assuming is because I've changed the ActionView
. Is there anyway I can do this whilst retaining the button function?
推荐答案
下面是创建自定义的例子菜单项
为动作条
。
Here's an example of creating a custom MenuItem
for the ActionBar
.
第一:创建查看
,将作为您的菜单项
。
First: Create the View
that will serve as your MenuItem
.
ActionLayoutExample
/**
* A {@link RelativeLayout} that serves as a custom {@link MenuItem}
*/
public class ActionLayoutExample extends RelativeLayout {
/** The MenuItem */
private TextView mHello;
/**
* Constructor for <code>ActionLayoutExample</code>
*
* @param context The {@link Context} to use
* @param attrs The attributes of the XML tag that is inflating the view
*/
public ActionLayoutExample(Context context, AttributeSet attrs) {
super(context, attrs);
// Ensure the MenuItem is accessible
CheatSheet.setup(this);
}
/**
* {@inheritDoc}
*/
@Override
protected void onFinishInflate() {
super.onFinishInflate();
// Initialize the TextView
mHello = (TextView) findViewById(android.R.id.text1);
mHello.setTypeface(myTypeface);
mHello.setText("Hello");
}
}
二:创建,你会真正应用到菜单项
的布局。
Second: Create the layout that you'll actually apply to the MenuItem
.
action_layout_example
<your.package.name.ActionLayoutExample xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/actionButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:contentDescription="@string/hello_world" >
<TextView
android:id="@android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</your.package.name.ActionLayoutExample>
这是重要的,包括样式风格=机器人:ATTR / actionButtonStyle
,以确保布局中正确反映动作条
项目。同样重要的是要包括安卓contentDescription
的布局。通常情况下,当你长时间preSS一个菜单项
有一个图标,工具提示显示,以表明该菜单项
是对于。在你的情况,你需要采取额外的步骤,以确保该提示是仍然可以访问。
It's important to include the style style="?android:attr/actionButtonStyle"
to make sure the layout properly reflects ActionBar
items. It's also important to include a android:contentDescription
in your layout. Normally when you long press a MenuItem
with an icon, a tooltip is shown to indicate what that MenuItem
is for. In your case, you'll need to take an extra step to make sure this tooltip is still accessible.
有关这方面的一个伟大的帮手是罗马Nurik的的cheatsheet 。你可以看到我使用它的操作布局的构造。
A great helper for this is Roman Nurik's CheatSheet. You can see how I'm using it in the constructor of the action layout.
您专门询问了菜单项
是可选择的。要做到这一点,请确保您包括安卓背景=机器人:ATTR / selectableItemBackground
和机器人:可点击=真正的
属性。
You specifically asked about the MenuItem
being selectable. To do this, make sure you include the android:background="?android:attr/selectableItemBackground"
and android:clickable="true"
attributes.
第三:使用安卓actionLayout
属性来设置你的布局和膨胀的菜单通常是您活动中的
或片段
。
Third: Use the android:actionLayout
attribute to set your layout and inflate the menu normally within your Activity
or Fragment
.
your_menu
<item
android:id="@+id/action_typeface"
android:actionLayout="@layout/action_layout_example"
android:showAsAction="ifRoom"
android:title="@string/hello_world"/>
使用菜单项
Using the MenuItem
在 onCreateOptionsMenu
呼叫 MenuItem.getActionView
和 View.onClickListener
。这是因为 onOptionsItemSelected
将不会被要求进行定制查看
In onCreateOptionsMenu
call MenuItem.getActionView
and on View.onClickListener
. This is because onOptionsItemSelected
will not be called for a custom View
final View example = menu.findItem(R.id.action_typeface).getActionView();
example .setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Do something
}
});
结果
相关推荐
最新文章