如何在Android操作栏菜单项中显示文本周围的边框?

Var*_*tia 1 android android-menu android-actionbar android-drawable android-background

我打算将类似Chrome的功能添加到我的android应用程序中(请参见下面的屏幕快照中的箭头),在操作栏菜单中会显示一个数字,并带有边框。我怎样才能做到这一点?

在此处输入图片说明

kri*_*son 5

让我们从框框开始:

/res/drawable/count_frame.xml

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
       android:inset="2dp">
    <shape
        android:shape="rectangle">

        <corners android:radius="2dp"/>
        <solid android:color="@android:color/transparent"/>
        <stroke
            android:width="2dp"
            android:color="#FF404040"/>
    </shape>

</inset>
Run Code Online (Sandbox Code Playgroud)

这个count_frame盒子会绕着一个TextView

/res/layout/menu_action_count_view.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/text"
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="24dp"
          android:layout_height="24dp"
          android:layout_margin="12dp"
          android:background="@drawable/count_frame"
          android:gravity="center"
          android:textColor="#FF000000"
          android:textSize="13sp"
          android:textStyle="bold"
          tools:text="4"/>
Run Code Online (Sandbox Code Playgroud)

TextView将是菜单项的操作视图。(app:由于我假设您正在使用,所以使用名称空间AppCompatActivity):

/res/menu/menu_main.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/action_result_view"
        android:title="@string/count"
        app:actionLayout="@layout/menu_action_count_view"
        app:showAsAction="always"/>

</menu>
Run Code Online (Sandbox Code Playgroud)

现在,在您的onCreateOptionsMenu替代中,您将获得动作视图并进行设置。假设您的数量在private int mCount;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    TextView count = (TextView) menu.findItem(R.id.action_result_view).getActionView();
    count.setText(Integer.toString(mCount));  // so the int isn't mistaken for a resource id!
    count.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // do your action here
        }
    });
    return true;
}
Run Code Online (Sandbox Code Playgroud)

计数更改时,请致电supportInvalidateOptionsMenu()

如果您想在带有边框的textview的水龙头上显示溢出菜单,请在 onCreateOptionsMenu

@Override
public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        final Menu m = menu;
        final MenuItem item = menu.findItem((R.id.action_result_view));
        TextView count = (TextView) menu.findItem(R.id.action_result_view).getActionView();
        count.setText(Integer.toString(mCount));  // so the int isn't mistaken for a resource id!
        count.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                m.performIdentifierAction(item.getItemId(), 0);
            }
        });
        return true;
    }
Run Code Online (Sandbox Code Playgroud)