【问题标题】:Style attributes of custom styled AlertDialog自定义样式的 AlertDialog 的样式属性
【发布时间】:2011-09-04 21:53:51
【问题描述】:

我正在尝试设置我的 AlertDialog 的样式,并且我已经能够通过样式和 xml 声明来更改其中的大部分内容......但仍然存在一些问题:

  1. 如何将标题栏周围的区域从黑色更改为我的自定义颜色?
  2. 如何将外部背景更改为透明(阴影落在外部的蓝色部分)
  3. 如何更改按钮,使其不与警报消息周围的黑色边框重叠?

这是我的 RootActivity 中的功能(我的活动扩展了这个)

public static void showNoConnectionDialog(Context ctx1) {
    final Context ctx = ctx1;
    LayoutInflater factory = LayoutInflater.from(ctx);
    AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(ctx, R.style.SetdartDialog));
    builder.setView(factory.inflate(R.layout.alert_dialog, null))
    .setIcon(R.drawable.icon)
    .setCancelable(true)
    .setMessage(R.string.check_wireless_settings)
    .setTitle(R.string.no_connection)
    .setPositiveButton(R.string.myes, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            ctx.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
        }
    })
    .setNegativeButton(R.string.mno, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            return;
        }
    })
    .setOnCancelListener(new DialogInterface.OnCancelListener() {
        public void onCancel(DialogInterface dialog) {
            return;
        }
    })
    .show();
}

这里是来自 styles.xml 的 sn-p

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme.WhiteBackground" parent="android:Theme">
        <item name="android:windowBackground">@null</item>
        <item name="android:background">@android:color/white</item>
        <!-- Dialog attributes
        <item name="alertDialogStyle">@style/AlertDialog</item>  -->
    </style>
    <style name="SetdartDialog">
            <item name="android:background">@color/sd_blue</item> <!-- MUST HAVE with white bg-->
            <!--<item name="android:windowBackground">@color/sd_blue</item> -->
            <!--<item name="android:windowBackground">@color/transparent</item> needed with white bg ? -->
            <item name="android:windowFrame">@color/transparent</item><!-- not sure what this changes-->
            <item name="android:textColor">@android:color/black</item>
            <item name="android:windowNoTitle">true</item>
            <item name="android:textSize">10sp</item>
            <item name="android:windowIsFloating">true</item>
            <item name="android:windowContentOverlay">@color/transparent</item>
            <item name="android:windowTitleStyle">@style/setwindowTitleStyle</item>
            <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
            <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
            <item name="android:backgroundDimEnabled">true</item>
            <item name="android:gravity">center_vertical|center_horizontal</item>
        <!--<item name="android:colorBackgroundCacheHint">@android:color/white</item>-->
        </style>
        <style name="setwindowTitleStyle">
            <item name="android:textColor">@android:color/white</item>
            <item name="android:background">@color/sd_blue</item>
        </style>
</resources>

还有 R.layout.alert_dialog

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/screen"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:orientation="vertical"
    >
    <LinearLayout
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:orientation="vertical">
    </LinearLayout>
</ScrollView>

【问题讨论】:

    标签: android android-layout dialog styles android-alertdialog


    【解决方案1】:

    使用您提到的所有这些属性创建您的自定义布局。使用Dialog 而不是AlertDialog,膨胀你创建的布局,使用Dialog对象来设置膨胀的布局。如果您还没有被介绍过充气服务,请做一些研究。在你清楚充气后,请记住你使用View 对象访问的对话框的所有组件,都是你用充气创建的。其余的(如点击监听器)仍需按常规方式完成。干杯。希望对你有帮助。

    【讨论】:

    • 这是 AlertDialog 类的限制还是错误?似乎我可以设置除我提到的问题之外的所有其他内容,我可以在层次结构查看器中看到他们的 id 是否可以以编程方式操作这些?你的权利我不太了解夸大观点,有人可以建议资源来理解这个概念而不是 RTFM 吗?
    • 我找到了几个学习膨胀的资源......谢谢。那么我的问题是错误还是未记录的功能? :)
    • 它对AD的限制。当然,在 AD 和 D 中,您都可以操作组件视图。您提到的功能没有记录,因为它们是自定义的,只记录了方法和方式,例如膨胀、访问子视图等......干杯
    • 对于任何关注这个的人,这里有两个我发现我喜欢的资源stackoverflow.com/questions/5026926/…curious-creature.org/2009/03/01/…
    【解决方案2】:

    要制作自定义 AlertDialog,您应该扩展 DialogFragment

    【讨论】:

      猜你喜欢
      • 2023-04-09
      • 2015-01-23
      • 2011-01-11
      • 2018-04-20
      • 2018-05-08
      • 1970-01-01
      • 1970-01-01
      • 2011-12-12
      • 1970-01-01
      相关资源
      最近更新 更多