gpt4 book ai didi

android - 替换Android DialogFragment的按钮背景

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:31:47 26 4
gpt4 key购买 nike

所以我之前在另一个线程的帮助下解决了这个问题,但是由于我的应用程序发生了一些新变化,以前的解决方案不再有效。这是上一个线程:Android App: Replace default button background with custom background in a Dialog Fragment

我正在尝试为自定义 DialogFragment 的正/负按钮的背景可绘制对象设置样式,但似乎无法进行任何更改。最初,我使用了以下内容:

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

这很好用,完全符合我的要求。但是,我认为这停止工作了,因为我最近添加了一个由 Android Action Bar 样式生成器创建的样式,在这里可以找到: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>

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

我在 onStart 方法中尝试的编程技巧似乎也没有让任何东西“粘”在按钮上。

我在这里找到了这个博客:http://android.codeandmagic.org/why-android-dialogfragment-confuses-me-part1/#comment-28043准确地描述了我的问题,只是它们走向了不同的方向并且从不回答我遇到的问题。

有什么想法可以尝试吗?

编辑:我还有一个未使用的 styles.xml。这是请求的其余文件。

这是我的 Android list :

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

这是我的整个 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>

最佳答案

设置默认 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>

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

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

在您创建构建器的 fragment 扩展中,将对客户主题的引用传递给它:

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

基本上就是这样。

注意事项: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/values/themes.xml

跟踪通常会从布局开始,然后根据属性、样式或主题进行跟踪。

我会坦率地承认,一旦我想出了如何做到这一点......去年的某个时候,我停止了挖掘,所以我无法完全解释为什么不允许在创建后修改 DialogFragment 的样式,但引用文档可以在传入 setStyle 方法条目时提及它。奇怪的是,在调用 onCreate 之后,这些样式似乎是不可变的。

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

关于android - 替换Android DialogFragment的按钮背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24691192/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com