替换Android DialogFragment的按钮背景

Jun*_*ter 5 android android-button android-dialogfragment

所以我以前解决了这个问题,感谢另一个线程的帮助,但由于我的应用程序的一些新的更改,以前的解决方案不再有效.这是上一个帖子:Android App:在对话框片段中用自定义背景替换默认按钮背景

我正在尝试设置自定义DialogFragment的正/负按钮的背景drawable,并且似乎无法获得任何更改.最初,我使用了以下内容:

@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));
}
Run Code Online (Sandbox Code Playgroud)

这非常有效,完全符合我的要求.但是,我认为这停止了​​工作,因为我最近添加了一个由Android Action Bar Style生成器创建的样式:http://jgilfelt.github.io/android-actionbarstylegenerator

我已经尝试将我的样式添加到动作栏样式生成器生成的样式文件中,但这不起作用.这是我尝试添加的内容:

<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>
Run Code Online (Sandbox Code Playgroud)

这些是我用http://android-holo-colors.com/生成的新绘图

我在onStart方法中尝试的程序性技巧似乎没有任何东西可以"粘"在按钮上.

我在这里找到了这个博客:http://android.codeandmagic.org/why-android-dialogfragment-confuses-me-part1/#comment-28043,它完全描述了我的问题,除了他们走向不同的方向,从不回答问题我有.

尝试其他事情的想法?

编辑:我也有一个没有使用的styles.xml.以下是所请求的其余文件.

这是我的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>
Run Code Online (Sandbox Code Playgroud)

这是我的整个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>
Run Code Online (Sandbox Code Playgroud)

Mat*_* T. 6

样式化默认的AlertDialog是非常棘手的,并没有真正支持,它表示这是可能的,有一些小的警告.

style.xml中为对话框添加自定义主题条目:

<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>
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)

在您创建构建器的片段扩展中,传递对custome主题的引用:

builder = new AlertDialog.Builder(getActivity(), R.style.CustomTheme_Dialog);
Run Code Online (Sandbox Code Playgroud)

基本上就是这样.

注意事项: AlertDialog的样式和主题不公开,因此它们不能用作新主题的父条目.为了"正确"扩展它,你必须复制Dialog和AlertDialog的样式之间的差异.这确实不是一个大问题,但您应该知道,专门触摸AlertDialog的平台样式的变化可能无法在没有干预的情况下完全转换到您的应用程序; 请记住一些事情.

不幸的是,为了真正了解这是如何工作的,您真的需要深入了解Android源代码并查看所有文件的应用方式. 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/值/的themes.xml

跟踪通常会从布局开始,然后依赖于attr,style或theme.

我会自由地承认,一旦我弄清楚如何做到这一点...去年的某个时候,我停止了挖掘,所以我无法完全解释为什么在创建后修改DialogFragment的样式是不允许的,但参考文档确实提到了它在setStyle方法条目中传递.在调用onCreate之后,这些样式似乎是奇怪的不可变.

如果您想扩展任何内容,请告诉我.