自定义风格AlertDialog的样式属性 [英] Style attributes of custom styled AlertDialog

查看:221
本文介绍了自定义风格AlertDialog的样式属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的风格和AlertDialog我已经能够通过样式和XML声明来改变大部分...但仍然有几个问题:

  1. 如何从周围的黑色标题栏区域而改变我的自定义颜色?
  2. 如何改变外层背景为透明(在外面的部分是蓝色的影子下降时)
  3. 如何更改按钮,这样他们就黑边框的警告消息不重叠?

下面是函数我在我的RootActivity(我的活动扩展这个)

 公共静态无效showNoConnectionDialog(上下文CTX1){
    最后上下文CTX = CTX1;
    LayoutInflater厂= LayoutInflater.from(CTX);
    AlertDialog.Builder建设者=新AlertDialog.Builder(新ContextThemeWrapper(CTX,R.style.SetdartDialog));
    builder.setView(factory.inflate(R.layout.alert_dialog,NULL))
    .setIcon(R.drawable.icon)
    .setCancelable(真)
    .setMessage(R.string.check_wireless_settings)
    .setTitle(R.string.no_connection)
    .setPositiveButton(R.string.myes,新DialogInterface.OnClickListener(){
        公共无效的onClick(DialogInterface对话,诠释它){
            ctx.startActivity(新意图(Settings.ACTION_WIRELESS_SETTINGS));
        }
    })
    .setNegativeButton(R.string.mno,新DialogInterface.OnClickListener(){
        公共无效的onClick(DialogInterface对话,诠释它){
            返回;
        }
    })
    .setOnCancelListener(新DialogInterface.OnCancelListener(){
        公共无效OnCancel的(DialogInterface对话){
            返回;
        }
    })
    。显示();
}
 

在这里从styles.xml片段

 < XML版本=1.0编码=UTF-8&GT?;
<资源>
    <样式名称=Theme.WhiteBackground父=安卓主题>
        <项目名称=机器人:windowBackground> @空< /项目>
        <项目名称=机器人:背景> @android:彩色/白< /项目>
        &所述;! - 对话框属性
        <项目名称=alertDialogStyle> @风格/ AlertDialog< /项目> - >
    < /风格>
    <样式名称=SetdartDialog>
            <项目名称=机器人:背景> @色/ sd_blue< /项目> <! - 必须用白色BG已有 - >
            !<  - <项目名称=机器人:windowBackground> @色/ sd_blue< /项目> - >
            !<  - <项目名称=机器人:windowBackground> @色/透明< /项目>需要用白色BG? - >
            <项目名称=!机器人:窗框> @色/透明< /项目><  - 不知道这是什么变化 - >
            <项目名称=机器人:文字颜色> @android:彩色/黑白LT; /项目>
            <项目名称=机器人:windowNoTitle>真< /项目>
            <项目名称=机器人:TEXTSIZE> 10SP< /项目>
            <项目名称=机器人:windowIsFloating>真< /项目>
            <项目名称=机器人:windowContentOverlay> @色/透明< /项目>
            <项目名称=机器人:windowTitleStyle> @风格/ setwindowTitleStyle< /项目>
            <项目名称=机器人:windowAnimationStyle> @android:款式/ Animation.Dialog< /项目>
            <项目名称=机器人:windowSoftInputMode> stateUnspecified | adjustPan< /项目>
            <项目名称=机器人:backgroundDimEnabled>真< /项目>
            <项目名称=安卓重力> center_vertical | center_horizo​​ntal< /项目>
        !<  - <项目名称=机器人:colorBackgroundCacheHint> @android:彩色/白< /项目>  - >
        < /风格>
        <样式名称=setwindowTitleStyle>
            <项目名称=机器人:文字颜色> @android:彩色/白< /项目>
            <项目名称=机器人:背景> @色/ sd_blue< /项目>
        < /风格>
< /资源>
 

此外R.layout.alert_dialog

 < XML版本=1.0编码=UTF-8&GT?;
<滚动型的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android机器人:ID =@ + ID /屏幕
    机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT
    机器人:方向=垂直
    >
    <的LinearLayout
        机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT
        机器人:方向=垂直>
    < / LinearLayout中>
< /滚动型>
 

解决方案

与所有你所提到的这些属性创建自定义布局。使用对话框而不是 AlertDialog ,抬高你已经创建的布局,使用Dialog对象设置充气布局。如果你还没有被引入到膨胀的服务,做一些研究。当你清楚与膨胀,记住,你访问的查看的对象,你已经用充气创建对话框的所有组件。其余的(如点击听众)仍然要在通常的方式进行。干杯。我希望它能帮助。

I am trying to style my AlertDialog and I have been able to change most of it through styles and xml declarations... but there are still a few issues:

  1. How do I change the area around the title bar from black to my custom color?
  2. How do I change the outer background to transparent (the outside part that is blue the the shadow drops upon)
  3. How do I change the buttons so they do not overlap the black border around the alert message?

here is the function I have in my RootActivity (my activities extend this one)

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();
}

here a snippet from styles.xml

<?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>

Also 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>

解决方案

Create your custom layout with all these attributes you've mentioned. Use Dialog instead of AlertDialog, inflate the layout you have created, use the Dialog object to set the inflated layout. If you haven't been introduced to inflating service, do some research. After you are clear with inflating, remember that all the components of the dialog you access with the View object, that you have created with the inflating. The rest (like click listeners) remains to be done on usual way. Cheers. I hope it helps.

这篇关于自定义风格AlertDialog的样式属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆