更换的Andr​​oid DialogFragment的按钮背景 [英] Replace Button Background of Android DialogFragment

查看:550
本文介绍了更换的Andr​​oid DialogFragment的按钮背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我previously解决了这个问题由于另一个线程的帮助,但由于一些新的变化,以我的应用程序的previous解决方案不再有效。这里的previous主题:<一href=\"http://stackoverflow.com/questions/20389663/android-app-replace-default-button-background-with-custom-background-in-a-dialo\">Android应用:替换默认按钮背景与对话片段自定义背景

我想要的风格定制DialogFragment的正/负按钮的背景图形内容和似乎无法得到任何改变坚持。本来,我用下面的:

  @覆盖
公共无效调用onStart(){
    super.onStart();
    按钮pButton =((AlertDialog)getDialog())getButton(DialogInterface.BUTTON_POSITIVE)。
    按钮n按钮=((AlertDialog)getDialog())getButton(DialogInterface.BUTTON_NEGATIVE)。    pButton.setBackground(getResources()getDrawable(R.drawable.button_custom));
    nButton.setBackground(getResources()getDrawable(R.drawable.button_custom));
}

这美丽的工作,也正是我想要的。然而,我的认为的这个停止工作,因为我最近添加的是Android操作栏样式发生器产生一个风格在这里找到:的 http://jgilfelt.github.io/android-actionbarstylegenerator

我试着加入我的样式由动作栏样式生成器生成的样式文件,但不起作用。这是我尝试添加:

 &LT;样式名称=ButtonLegacyButton父=机器人:Widget.Holo.Light.Button&GT;
  &LT;项目名称=机器人:背景&GT; @绘制/ legacybutton_btn_default_holo_light&LT; /项目&GT;
&LT; /风格&GT;&LT;样式名称=ImageButtonLegacyButton父=机器人:Widget.Holo.Light.ImageButton&GT;
  &LT;项目名称=机器人:背景&GT; @绘制/ legacybutton_btn_default_holo_light&LT; /项目&GT;
&LT; /风格&GT;

这些都是我 http://android-holo-colors.com/

没有纲领性诡计我尝试在OnStart方法似乎得到任何的按钮大棒无论是。

我发现这个博客在这里:的http://android.$c$candmagic.org/why-android-dialogfragment-confuses-me-part1/#comment-28043它描述了我的问题的究竟的,除非他们去一个不同的方向,从来没有回答我的问题。

其他的事情任何想法来试试呢?

编辑:
我也有这不是正在使用styles.xml。这里有要求的其他文件。

下面是我的Andr​​oid清单:

 &LT;?XML版本=1.0编码=UTF-8&GT?;
&LT;清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.example.legacy
    安卓版code =1
    机器人:=的versionName1.0&GT;    &LT;用途-SDK
        安卓的minSdkVersion =16
        机器人:targetSdkVersion =19/&GT;    &lt;应用
        机器人:allowBackup =真
        机器人:图标=@绘制/ legacy_icon
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ Theme.Legacylogo&GT;
        &LT;活动
            机器人:名字=com.example.legacy.GameListActivity
            机器人:标签=@字符串/ APP_NAME&GT;
            &所述;意图滤光器&gt;
                &lt;作用机器人:名字=android.intent.action.MAIN/&GT;                &LT;类机器人:名字=android.intent.category.LAUNCHER/&GT;
            &所述; /意图滤光器&gt;
        &LT; /活性GT;
        ...等活动
    &LT; /用途&gt;&LT; /清单&GT;

下面是我的整个styles_legacylogo.xml:

 &LT;资源&GT;    &LT;样式名称=Theme.Legacylogo父=@风格/ Theme.AppCompat.Light&GT;
        &LT;项目名称=机器人:actionBarItemBackground&GT; @绘制/ selectable_background_legacylogo&LT; /项目&GT;
        &LT;项目名称=机器人:popupMenuStyle&GT; @风格/ PopupMenu.Legacylogo&LT; /项目&GT;
        &LT;项目名称=机器人:dropDownListViewStyle&GT; @风格/ DropDownListView.Legacylogo&LT; /项目&GT;
        &LT;项目名称=机器人:actionBarTabStyle&GT; @风格/ ActionBarTabStyle.Legacylogo&LT; /项目&GT;
        &LT;项目名称=机器人:actionDropDownStyle&GT; @风格/ DropDownNav.Legacylogo&LT; /项目&GT;
        &LT;项目名称=机器人:actionBarStyle&GT; @风格/ ActionBar.Transparent.Legacylogo&LT; /项目&GT;
        &LT;项目名称=机器人:actionModeBackground&GT; @绘制/ cab_background_top_legacylogo&LT; /项目&GT;
        &LT;项目名称=机器人:actionModeSplitBackground&GT; @绘制/ cab_background_bottom_legacylogo&LT; /项目&GT;
        &LT;项目名称=机器人:actionModeCloseButtonStyle&GT; @风格/ ActionButton.CloseMode.Legacylogo&LT; /项目&GT;
        &LT;项目名称=机器人:spinnerItemStyle&GT; @风格/ SpinnerItem&LT; /项目&GT;
        &LT;项目名称=机器人:spinnerDropDownItemStyle&GT; @风格/ SpinnerItem.DropDownItem&LT; /项目&GT;
    &LT; /风格&GT;    &LT;样式名称=ActionBar.Solid.Legacylogo父=@安卓风格/ Widget.Holo.ActionBar.Solid&GT;
        &LT;项目名称=机器人:背景&GT; @绘制/ ab_solid_legacylogo&LT; /项目&GT;
        &LT;项目名称=机器人:backgroundStacked&GT; @绘制/ ab_stacked_solid_legacylogo&LT; /项目&GT;
        &LT;项目名称=机器人:backgroundSplit&GT; @绘制/ ab_bottom_solid_legacylogo&LT; /项目&GT;
        &LT;项目名称=机器人:progressBarStyle&GT; @风格/ ProgressBar.Legacylogo&LT; /项目&GT;
    &LT; /风格&GT;    &LT;样式名称=ActionBar.Transparent.Legacylogo父=@安卓风格/ Widget.Holo.ActionBar&GT;
        &LT;项目名称=机器人:背景&GT; @绘制/ ab_transparent_legacylogo&LT; /项目&GT;
        &LT;项目名称=机器人:progressBarStyle&GT; @风格/ ProgressBar.Legacylogo&LT; /项目&GT;
    &LT; /风格&GT;    &LT;样式名称=PopupMenu.Legacylogo父=@安卓风格/ Widget.Holo.ListPopupWindow&GT;
        &LT;项目名称=机器人:popupBackground&GT; @绘制/ menu_dropdown_panel_legacylogo&LT; /项目&GT;
    &LT; /风格&GT;    &LT;样式名称=DropDownListView.Legacylogo父=@安卓风格/ Widget.Holo.ListView.DropDown&GT;
        &LT;项目名称=机器人:listSelector&GT; @绘制/ selectable_background_legacylogo&LT; /项目&GT;
    &LT; /风格&GT;    &LT;样式名称=ActionBarTabStyle.Legacylogo父=@安卓风格/ Widget.Holo.ActionBar.TabView&GT;
        &LT;项目名称=机器人:背景&GT; @绘制/ tab_indicator_ab_legacylogo&LT; /项目&GT;
    &LT; /风格&GT;    &LT;样式名称=DropDownNav.Legacylogo父=@安卓风格/ Widget.Holo.Spinner&GT;
        &LT;项目名称=机器人:背景&GT; @绘制/ spinner_background_ab_legacylogo&LT; /项目&GT;
        &LT;项目名称=机器人:popupBackground&GT; @绘制/ menu_dropdown_panel_legacylogo&LT; /项目&GT;
        &LT;项目名称=机器人:dropDownSelector&GT; @绘制/ selectable_background_legacylogo&LT; /项目&GT;
    &LT; /风格&GT;    &LT;样式名称=ProgressBar.Legacylogo父=@安卓风格/ Widget.Holo.ProgressBar.Horizo​​ntal&GT;
        &LT;项目名称=机器人:progressDrawable&GT; @绘制/ progress_horizo​​ntal_legacylogo&LT; /项目&GT;
    &LT; /风格&GT;    &LT;样式名称=ActionButton.CloseMode.Legacylogo父=@安卓风格/ Widget.Holo.ActionButton.CloseMode&GT;
        &LT;项目名称=机器人:背景&GT; @绘制/ btn_cab_done_legacylogo&LT; /项目&GT;
    &LT; /风格&GT;    &LT;样式名称=SpinnerItem父=@安卓风格/ Widget.TextView.SpinnerItem&GT;
        &LT;项目名称=机器人:文字颜色&GT;#fef7e7&LT; /项目&GT;
    &LT; /风格&GT;    &LT;样式名称=SpinnerItem.DropDownItem父=@安卓风格/ Widget.DropDownItem.Spinner&GT;
        &LT;项目名称=机器人:文字颜色&GT;#fef7e7&LT; /项目&GT;
    &LT; /风格&GT;    &LT;样式名称=ButtonLegacyButton父=机器人:Widget.Holo.Light.Button&GT;
      &LT;项目名称=机器人:背景&GT; @绘制/ legacybutton_btn_default_holo_light&LT; /项目&GT;
    &LT; /风格&GT;    &LT;样式名称=ImageButtonLegacyButton父=机器人:Widget.Holo.Light.ImageButton&GT;
      &LT;项目名称=机器人:背景&GT; @绘制/ legacybutton_btn_default_holo_light&LT; /项目&GT;
    &LT; /风格&GT;    &LT;! - 这种风格只在Light.DarkActionBar基于主题引用 - &GT;
    &LT;样式名称=Theme.Legacylogo.Widget父=@安卓风格/ Theme.Holo&GT;
        &LT;项目名称=机器人:popupMenuStyle&GT; @风格/ PopupMenu.Legacylogo&LT; /项目&GT;
        &LT;项目名称=机器人:dropDownListViewStyle&GT; @风格/ DropDownListView.Legacylogo&LT; /项目&GT;
    &LT; /风格&GT;&LT; /资源&GT;


解决方案

的样式默认AlertDialog是非常棘手,而不是真正的支持,认为这是可能的,有一些小的警告。

在您的 style.xml 的添加对对话的自定义主题条目:

 &LT;样式名称=CustomTheme.Dialog父=机器人:Theme.Holo.Light.Dialog&GT;
    &LT;项目名称=机器人:buttonBarButtonStyle&GT; @风格/ CustomTheme.Dialog.Button&LT; /项目&GT;
    &LT;! - 见警告 - &GT;
    &LT;项目名称=机器人:windowBackground&GT; @android:彩色/透明&LT; /项目&GT;
    &LT;项目名称=机器人:windowContentOverlay&GT; @空&LT; /项目&GT;
    &LT;项目名称=机器人:windowMinWidthMajor&GT; @android:扪/ dialog_min_width_major&LT; /项目&GT;
    &LT;项目名称=机器人:windowMinWidthMinor&GT; @android:扪/ dialog_min_width_minor&LT; /项目&GT;
&LT; /风格&GT;

然后添加一个样式按钮本身:

 &LT;样式名称=CustomTheme.Dialog.Button父=@安卓风格/ Widget.Holo.Light.Button.Borderless.Small&GT;
    &LT;项目名称=机器人:背景&GT; @绘制/ legacybutton_btn_default_holo_light&LT; /项目&GT;
&LT; /风格&GT;

在那里你创建构建器传递给它参考custome主题的分片扩展:

  =建设者新AlertDialog.Builder(getActivity(),R.style.CustomTheme_Dialog);

从本质上讲,就是这样。

注意事项:
风格和AlertDialog的主题不是公共所以它们不能被用作一个新的主题的父条目。为了正确的扩展它,你必须复制的样式对话框和AlertDialog之间的差异。这真的不是一个巨大的问题,但你应该知道,在专门摸AlertDialog可能不完全过渡到您的应用程序无需干预平台的造型变化;只是要记住。

不幸的是,要真正了解其工作原理,你真的需要通过Android源挖,看看如何应用的所有文件。
<一href=\"https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/app/AlertDialog.java\" rel=\"nofollow\">https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/app/AlertDialog.java
<一href=\"https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/layout/alert_dialog_holo.xml\" rel=\"nofollow\">https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/layout/alert_dialog_holo.xml
<一href=\"https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/values/attrs.xml\" rel=\"nofollow\">https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/values/attrs.xml
<一href=\"https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/values/styles.xml\" rel=\"nofollow\">https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/values/styles.xml
<一href=\"https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/values/themes.xml\" rel=\"nofollow\">https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/values/themes.xml

一般会跟踪根据先从布局再进ATTR,风格或主题。

我会坦率地承认,一旦我想通了,如何做到这一点的某个时候......去年,我停止了挖,所以我不能完全解释为什么修改DialogFragment的风格在创建后是不允许的,但参考文档做使得它提传入的setStyle方法入口。这些风格似乎是不可变的奇怪的onCreate被调用后。

请让我知道你想什么扩大了。

So I previously solved this problem thanks to the help of another thread, but due to some new changes to my app the previous solution no longer works. Here's the previous thread: Android App: Replace default button background with custom background in a Dialog Fragment

I'm trying to style the background drawables of the positive/negative buttons of a custom DialogFragment, and cannot seem to get any change to stick. Originally, I used the following:

@Override
public void onStart() {
    super.onStart();
    Button pButton =  ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_POSITIVE);
    Button nButton =  ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_NEGATIVE);

    pButton.setBackground(getResources().getDrawable(R.drawable.button_custom));
    nButton.setBackground(getResources().getDrawable(R.drawable.button_custom));
}

This worked beautifully and did exactly what I wanted. However, I think that this stopped working because I recently added a style created by the Android Action Bar Style generator found here: http://jgilfelt.github.io/android-actionbarstylegenerator

I've tried adding my styles to the style file generated by the action bar style generator, but that doesn't work. Here's what I tried adding:

<style name="ButtonLegacyButton" parent="android:Widget.Holo.Light.Button">
  <item name="android:background">@drawable/legacybutton_btn_default_holo_light</item>
</style>

<style name="ImageButtonLegacyButton" parent="android:Widget.Holo.Light.ImageButton">
  <item name="android:background">@drawable/legacybutton_btn_default_holo_light</item>
</style>

Those are new drawables that I generated with http://android-holo-colors.com/

No programmatic trickery I try in the onStart method seems to get anything to "stick" on the buttons either.

I found this blog here: http://android.codeandmagic.org/why-android-dialogfragment-confuses-me-part1/#comment-28043 which describes my problem exactly, except they go in a different direction and never answer the problem I have.

Any ideas of other things to try?

EDIT: I also have a styles.xml that's not being used. Here are the rest of the files requested.

Here is my Android Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.legacy"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/legacy_icon"
        android:label="@string/app_name"
        android:theme="@style/Theme.Legacylogo">
        <activity
            android:name="com.example.legacy.GameListActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        ... other activities
    </application>

</manifest>

Here is my entire styles_legacylogo.xml:

<resources>

    <style name="Theme.Legacylogo" parent="@style/Theme.AppCompat.Light">
        <item name="android:actionBarItemBackground">@drawable/selectable_background_legacylogo</item>
        <item name="android:popupMenuStyle">@style/PopupMenu.Legacylogo</item>
        <item name="android:dropDownListViewStyle">@style/DropDownListView.Legacylogo</item>
        <item name="android:actionBarTabStyle">@style/ActionBarTabStyle.Legacylogo</item>
        <item name="android:actionDropDownStyle">@style/DropDownNav.Legacylogo</item>
        <item name="android:actionBarStyle">@style/ActionBar.Transparent.Legacylogo</item>
        <item name="android:actionModeBackground">@drawable/cab_background_top_legacylogo</item>
        <item name="android:actionModeSplitBackground">@drawable/cab_background_bottom_legacylogo</item>
        <item name="android:actionModeCloseButtonStyle">@style/ActionButton.CloseMode.Legacylogo</item>
        <item name="android:spinnerItemStyle">@style/SpinnerItem</item>
        <item name="android:spinnerDropDownItemStyle">@style/SpinnerItem.DropDownItem</item>
    </style>

    <style name="ActionBar.Solid.Legacylogo" parent="@android:style/Widget.Holo.ActionBar.Solid">
        <item name="android:background">@drawable/ab_solid_legacylogo</item>
        <item name="android:backgroundStacked">@drawable/ab_stacked_solid_legacylogo</item>
        <item name="android:backgroundSplit">@drawable/ab_bottom_solid_legacylogo</item>
        <item name="android:progressBarStyle">@style/ProgressBar.Legacylogo</item>
    </style>

    <style name="ActionBar.Transparent.Legacylogo" parent="@android:style/Widget.Holo.ActionBar">
        <item name="android:background">@drawable/ab_transparent_legacylogo</item>
        <item name="android:progressBarStyle">@style/ProgressBar.Legacylogo</item>
    </style>

    <style name="PopupMenu.Legacylogo" parent="@android:style/Widget.Holo.ListPopupWindow">
        <item name="android:popupBackground">@drawable/menu_dropdown_panel_legacylogo</item>    
    </style>

    <style name="DropDownListView.Legacylogo" parent="@android:style/Widget.Holo.ListView.DropDown">
        <item name="android:listSelector">@drawable/selectable_background_legacylogo</item>
    </style>

    <style name="ActionBarTabStyle.Legacylogo" parent="@android:style/Widget.Holo.ActionBar.TabView">
        <item name="android:background">@drawable/tab_indicator_ab_legacylogo</item>
    </style>

    <style name="DropDownNav.Legacylogo" parent="@android:style/Widget.Holo.Spinner">
        <item name="android:background">@drawable/spinner_background_ab_legacylogo</item>
        <item name="android:popupBackground">@drawable/menu_dropdown_panel_legacylogo</item>
        <item name="android:dropDownSelector">@drawable/selectable_background_legacylogo</item>
    </style>

    <style name="ProgressBar.Legacylogo" parent="@android:style/Widget.Holo.ProgressBar.Horizontal">
        <item name="android:progressDrawable">@drawable/progress_horizontal_legacylogo</item>
    </style>

    <style name="ActionButton.CloseMode.Legacylogo" parent="@android:style/Widget.Holo.ActionButton.CloseMode">
        <item name="android:background">@drawable/btn_cab_done_legacylogo</item>
    </style>

    <style name="SpinnerItem" parent="@android:style/Widget.TextView.SpinnerItem">
        <item name="android:textColor">#fef7e7</item>
    </style>

    <style name="SpinnerItem.DropDownItem" parent="@android:style/Widget.DropDownItem.Spinner">
        <item name="android:textColor">#fef7e7</item>
    </style>

    <style name="ButtonLegacyButton" parent="android:Widget.Holo.Light.Button">
      <item name="android:background">@drawable/legacybutton_btn_default_holo_light</item>
    </style>

    <style name="ImageButtonLegacyButton" parent="android:Widget.Holo.Light.ImageButton">
      <item name="android:background">@drawable/legacybutton_btn_default_holo_light</item>
    </style>

    <!-- this style is only referenced in a Light.DarkActionBar based theme -->
    <style name="Theme.Legacylogo.Widget" parent="@android:style/Theme.Holo">
        <item name="android:popupMenuStyle">@style/PopupMenu.Legacylogo</item>
        <item name="android:dropDownListViewStyle">@style/DropDownListView.Legacylogo</item>
    </style>

</resources>

解决方案

Styling the default AlertDialog is extremely tricky and not really supported, that said it is possible, with some small caveats.

In your style.xml add a custom theme entry for the dialog:

<style name="CustomTheme.Dialog" parent="android:Theme.Holo.Light.Dialog">
    <item name="android:buttonBarButtonStyle">@style/CustomTheme.Dialog.Button</item>
    <!-- See caveat -->
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowMinWidthMajor">@android:dimen/dialog_min_width_major</item>
    <item name="android:windowMinWidthMinor">@android:dimen/dialog_min_width_minor</item>
</style>

Then add a style for the button itself:

<style name="CustomTheme.Dialog.Button" parent="@android:style/Widget.Holo.Light.Button.Borderless.Small">
    <item name="android:background">@drawable/legacybutton_btn_default_holo_light</item>
</style>

In your fragment extension where you create the builder pass it the reference to the custome theme:

builder = new AlertDialog.Builder(getActivity(), R.style.CustomTheme_Dialog);

Essentially that's it.

Caveats: The style and theme of AlertDialog are not public so they cannot be used as the parent entry of a new theme. In order to "correctly" extend it you have to duplicate the differences between the styles of Dialog and AlertDialog. This really isn't a huge issue but you should be aware that changes in the styling of the platform that specifically touch AlertDialog may not entirely transition to your app without intervention; Just something to keep in mind.

Unfortunately in order to really see how this works you really need to dig through the Android source and look at how all the files are applied. https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/app/AlertDialog.java https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/layout/alert_dialog_holo.xml https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/values/attrs.xml https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/values/styles.xml https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/values/themes.xml

Tracing generally would start with the layout then into attr, style or theme depending.

I will freely admit that once I figured out how to do this ... sometime last year, I stopped digging so I cannot fully explain why modifying the style of a DialogFragment after it is created is not allowed but the reference docs do make mention of it in passing in the setStyle method entry. Those styles seem to be oddly immutable after onCreate is called.

Please let me know if you would like anything expanded on.

这篇关于更换的Andr​​oid DialogFragment的按钮背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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