【问题标题】:How to add an customized button on the actionbar?如何在操作栏上添加自定义按钮?
【发布时间】:2015-08-25 19:29:59
【问题描述】:

我想制作一个操作栏,右侧有一个带有 margin_right 的按钮。像这样 我使用操作按钮来做到这一点。但是按钮在右边,没有margin_right。 android:layout_marginRight 在这里不起作用。

这是我的styles.xml:

<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <item name="android:actionBarSize">50dp</item>
    <item name="android:actionBarStyle">@style/my_actionbar_style</item>
    <item name="android:actionButtonStyle">@style/my_action_button</item>

</style>
<style name="my_actionbar_style" parent="android:Widget.ActionBar">
    <item name="android:background">@color/actionbar_backgroud</item>
    <item name="android:titleTextStyle">@style/myTitleStyle</item>
    <item name="android:displayOptions">showTitle</item>
</style>
<style name="myTitleStyle">
    <item name="android:textColor">#ffffff</item>
    <item name="android:textSize">20sp</item>
</style>

<style name="my_action_button">
    <item name="android:background">@drawable/button_selector</item>
    <item name="android:width">70dp</item>
    <item name="android:textSize">13sp</item>
    <item name="android:layout_marginTop">10dp</item>
    <item name="android:layout_marginBottom">10dp</item>
    <item name="android:layout_marginRight">10dp</item>
</style>

这是我的 menu.xml:

<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:ignore="AppCompatResource">

<item
    android:id="@+id/action_search"
    android:title="submit"
    android:showAsAction="always" /></menu>

【问题讨论】:

标签: android


【解决方案1】:

另一种快速而简洁的方法是为该菜单按钮指定您自己的布局。像这样的

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      tools:context="com.example.MainActivity">

    <item
        android:id="@+id/action_custom_button"
        android:title="Custom Button"
        android:icon="@drawable/ic_custom_button"
        app:actionLayout="@layout/layout_to_custom_button"
        app:showAsAction="always" />

</menu>

应该有

layout_to_custom_button.xml

包含您想要的填充和样式的布局文件。

并在您的活动中为点击事件执行此操作

@Override
public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.menu_product_detail, menu);
  final Menu mMenu = menu;
  final MenuItem item = menu.findItem(R.id.action_custom_button);
  item.getActionView().setOnClickListener(new View.OnClickListener(){

    @Override
    public void onClick(View v) {
      mMenu.performIdentifierAction(item.getItemId(), 0);
    }
   });
        return true;
}

【讨论】:

  • 成功了!多谢。但是我发现应该加"android:showAsAcion="always"",不然action按钮会隐藏在overflow中
  • 抱歉,我遇到了另一个问题。添加“actionLayout”后,公共布尔 onOptionsItemSelected(MenuItem item) 方法不起作用。尽管我单击了按钮,但似乎该方法没有被激活
  • 我刚刚添加了一个如何获取自定义菜单项的点击事件的方法。谢谢
【解决方案2】:

我发现最简单的方法就是在布局文件中创建自己的操作栏。然后,您可以通过让您的活动从覆盖 setContentView 的通用活动继承来使用它,您只需将您获得的视图粘贴到包含该视图和您的操作栏的父视图中。您可以使用 ActionMenuView (5.0+) 来模仿溢出的菜单行为。 例如,假设您有一个 action_bar.xml,其中包含一个名为 menuView 的 ActionMenuView:

class BaseActivity extends Activity {

    ...
    ActionMenuView mMenuView;
    ...

    @Override
    protected void setContentView(int resId) {
        LayoutInflater inflater = getLayoutInflater();
        View actionBar = inflater.inflate(R.layout.action_bar, null);
        View contentView = inflater.inflate(resId);

        mMenuView = (ActionMenuView)actionBar.findViewById(R.id.menuView);

        RelativeLayout parentLayout = new RelativeLayout(this);
        LayoutParams actionBarLayout = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        actionBar.setLayoutParams(actionBarLayout);
        parentLayout.addView(actionBar);

        actionBar.setId(R.id.actionBar);
        LayoutParams contentLayout = new LayoutParams(LayoutParams.MATCH_PARENT, 0);
        contentLayout.addRule(RelativeLayout.BELOW, actionBar.getId());          
        contentLayout.addRule(RelativeLayout.ALIGN_WITH_PARENT_BOTTOM);
        contentView.setLayoutParams(contentLayout);
        parentLayout.addView(contentView);

        setContentView(parentLayout);
    }

}

【讨论】:

  • 成功了!非常感谢!但是我更喜欢使用xml来解决这个问题:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-22
  • 1970-01-01
  • 1970-01-01
  • 2013-02-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多