【问题标题】:AppCompat theme losing all the blue title stylingAppCompat 主题失去了所有蓝色标题样式
【发布时间】:2016-04-15 05:04:49
【问题描述】:

我有一个包含 ListView 的 AppCompatActivity,我将其显示为对话框(即要求用户拍照或从图库中选择图像的列表)。

但是它说我必须为对话框使用Theme.AppCompat。所以我用

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

但是,与标准 DialogFragments 等中似乎出现的蓝色文本/蓝色下划线相比,生成的标题为黑色且没有下划线。

如何将相同的主题返回到此 Dialog 主题?

使用屏幕 sn-p 编辑:

我正在通过执行以下操作从另一个 DialogFragment 调用这个新对话框:

Intent intent = new Intent(getActivity(), CameraMenu.class);
intent.putExtra(TAG_1, stuff_1);
intent.putExtra(TAG_2, stuff_2);
startActivityForResult(intent, ACT_TAG);

【问题讨论】:

  • 使用活动并将其显示为对话框的任何特殊原因?因为您正在寻找可以通过使用 android.support.v7.app.AlertDialog.Builder 来实现,设置标题并提供 setSingleChoiceItems
  • @RajenRaiyarela 我在 DialogFragment 中,当我让我的用户选择从相机或图库中抓取图片时,该选择以嵌入在 Activity 中的 ListView 的形式授予,为了让这个活动显示为对话框,我使用了对话框主题。
  • 我使用 ListView 来显示选项,因为它允许我有效地包含每个选项的图标
  • @RajenRaiyarela 我的理解是SingleChoiceItems 不允许完全自定义的项目视图,就像你可以用 ListViews 做的那样(即不能像我在截图)
  • 如果仅用于显示列表项的图标和文本,那么对于简单的解决方案,而不是将 SingleChoiceItems 作为字符串数组传递,您可以使用您在活动中创建的相同适配器。由于 DialogFragment 样式支持中的问题,您没有获得所需的标题颜色。您必须使用 android.support.v7.app.AlertDialog 才能正确处理

标签: android android-appcompat android-theme android-styles android-dialog


【解决方案1】:

试试这个。

创建一个不给样式的对话框:

public static void showDialog(Activity activity) {

        final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        View view =  LayoutInflater.from(activity).inflate(R.layout.dialog_common, null);
        builder.setView(view);

        alert = builder.create();
        alert.show();

    }

【讨论】:

  • 我应该补充一点,现在我正在通过 Intent 启动此 Activity。它被编码为一个活动,但由于主题,它“看起来像”一个对话框(因此它没有占据屏幕,而是悬停在一个较小的盒子中的所有内容上)
  • 那你到底想要什么?你想显示 AppCompat 对话框吗?
  • 由于使用了 Dialog 主题,它已经显示为 Dialog,只是标题样式已减少为黑色,而不是典型的带下划线的蓝色
  • @KaliMa 用你想要的标题或对话框输出来拍摄你的屏幕。
  • @jaydroider 用截图添加到上面的帖子中
【解决方案2】:

这是您的对话框的示例主题,其中 colorAccent 将用于您的按钮,textColorPrimary 将是您的标题颜色,背景是您的对话框背景。

<style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
  <item name="colorAccent">#0900FF</item>
  <item name="android:textColorPrimary">#FFFFFF</item>
  <item name="android:background">#FF0049</item>
</style>

现在将此主题添加到您的应用程序 AppTheme

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- your AlertDialog style -->
    <item name="alertDialogTheme">@style/AlertDialogStyle</item>
</style>

现在创建您的对话框。这里 aa 是您在 Listview 中用作适配器的 ArrayAdapter 或 BaseAdapter。

import android.support.v7.app.AlertDialog

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity().this);
    builder.setTitle("Dialog");
    builder.setSingleChoiceItems(aa, 0, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dlg, int which) { 
       //which is index of your item clicked from the list. So take appropriate action.
     } 
    }) 
builder.show();

【讨论】:

  • 但是 Intent 参数和设置呢?图标呢?
  • 由于您将使用与列表视图相同的适配器,因此您将获得示例显示。关于 Intent,您可以检查 which 值并采取适当的措施来显示画廊或打开相机。不,您不需要打开您的活动。而是从您的对话框片段中显示此对话框并实现您的代码。
  • 代码崩溃了,它在 builder.show();存在“窗口泄漏”的行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-11
相关资源
最近更新 更多