【问题标题】:change popup menu header style更改弹出菜单标题样式
【发布时间】:2016-09-17 07:51:33
【问题描述】:

我有一个 android.View.ContextMenu,它在单击 recyclerview 中的项目后弹出。它看起来像这样:

我的问题是,我不知道如何更改标题(此处:12345678)和分隔线的颜色。

我尝试创建一个 PopupMenuStyle,但我只能更改项目,不能更改标题。我使用 Theme.AppCompat.Light.DarkActionBar 作为我的主题的父项

编辑 1:

有了这个,我可以将 headertextcolor 设置为主要文本颜色,但无论我添加哪些项目,分隔线仍然保持蓝色。

<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
...
</style>

编辑 2:

上下文菜单创建

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
     super.onCreateContextMenu(menu, v, menuInfo);
     MenuInflater inflater = getActivity().getMenuInflater();
     inflater.inflate(R.menu.my_menu, menu);

     menu.setHeaderTitle("12345678");
}

样式

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar" parent="@style/AppTheme">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>

    <item name="android:alertDialogTheme">@style/AppCompatAlertDialogStyle</item>
</style>

<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
     <!-- I tried a lot here, but nothing works -->
</style>

【问题讨论】:

标签: android android-styles


【解决方案1】:

更改分隔线颜色:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.dialog)
   .setIcon(R.drawable.ic)
   .setMessage(R.string.dialog_msg);
Dialog d = builder.show();
int dividerId = d.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
View divider = d.findViewById(dividerId);
divider.setBackgroundColor(getResources().getColor(R.color.my_color));

更改文本标题颜色:

public static void brandAlertDialog(AlertDialog dialog) {
    try {
        Resources resources = dialog.getContext().getResources();
        int color = resources.getColor(...); // your color here

        int alertTitleId = resources.getIdentifier("alertTitle", "id", "android");
        TextView alertTitle = (TextView) dialog.getWindow().getDecorView().findViewById(alertTitleId);
        alertTitle.setTextColor(color); // change title text color

        int titleDividerId = resources.getIdentifier("titleDivider", "id", "android");
        View titleDivider = dialog.getWindow().getDecorView().findViewById(titleDividerId);
        titleDivider.setBackgroundColor(color); // change divider color
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多