gpt4 book ai didi

android - 在android中自定义按钮

转载 作者:太空狗 更新时间:2023-10-29 16:19:31 26 4
gpt4 key购买 nike

我想自定义向上按钮,我正在为我的操作栏使用appcompat library。我不想要 android 提供的默认向上按钮。因为我正在使用由 style generator 生成的 actionbar .因此,向上按钮在我选择的 actionbar 颜色下显得苍白。有人可以建议我如何实现吗?我已经搜索了很多但找不到。

enter image description here

我已经为它准备了自己的形象。所以我想用我的图像替换它。我相信这是可能的。因为 googleplay 在应用程序部分中以不同颜色显示带有向上按钮的操作栏。

下面是 styles.xml:

     <style name="Theme.Customab" parent="@style/Theme.AppCompat.Light.DarkActionBar">
<item name="actionBarItemBackground">@drawable/selectable_background_customab</item>
<item name="popupMenuStyle">@style/PopupMenu.Customab</item>
<item name="dropDownListViewStyle">@style/DropDownListView.Customab</item>
<item name="actionBarTabStyle">@style/ActionBarTabStyle.Customab</item>
<item name="actionDropDownStyle">@style/DropDownNav.Customab</item>
<item name="actionBarStyle">@style/ActionBar.Solid.Customab</item>
<item name="actionModeBackground">@drawable/cab_background_top_customab</item>
<item name="actionModeSplitBackground">@drawable/cab_background_bottom_customab</item>
<item name="actionModeCloseButtonStyle">@style/ActionButton.CloseMode.Customab</item>

<!-- Light.DarkActionBar specific -->
<item name="actionBarWidgetTheme">@style/Theme.Customab.Widget</item>

**<item name="android:homeAsUpIndicator">@drawable/customup</item>**


</style>

<style name="ActionBar.Solid.Customab" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="background">@drawable/ab_solid_customab</item>
<item name="backgroundStacked">@drawable/ab_stacked_solid_customab</item>
<item name="backgroundSplit">@drawable/ab_bottom_solid_customab</item>
<item name="progressBarStyle">@style/ProgressBar.Customab</item>
</style>

<style name="ActionBar.Transparent.Customab" parent="@style/Widget.AppCompat.ActionBar">
<item name="background">@drawable/ab_transparent_customab</item>
<item name="progressBarStyle">@style/ProgressBar.Customab</item>
</style>

<style name="PopupMenu.Customab" parent="@style/Widget.AppCompat.PopupMenu">
<item name="android:popupBackground">@drawable/menu_dropdown_panel_customab</item>
</style>

<style name="DropDownListView.Customab" parent="@style/Widget.AppCompat.ListView.DropDown">
<item name="android:listSelector">@drawable/selectable_background_customab</item>
</style>

<style name="ActionBarTabStyle.Customab" parent="@style/Widget.AppCompat.ActionBar.TabView">
<item name="android:background">@drawable/tab_indicator_ab_customab</item>
</style>

<style name="DropDownNav.Customab" parent="@style/Widget.AppCompat.Spinner.DropDown.ActionBar">
<item name="android:background">@drawable/spinner_background_ab_customab</item>
<item name="android:popupBackground">@drawable/menu_dropdown_panel_customab</item>
<item name="android:dropDownSelector">@drawable/selectable_background_customab</item>
</style>

<style name="ProgressBar.Customab" parent="@style/Widget.AppCompat.ProgressBar.Horizontal">
<item name="android:progressDrawable">@drawable/progress_horizontal_customab</item>
</style>

<style name="ActionButton.CloseMode.Customab" parent="@style/Widget.AppCompat.ActionButton.CloseMode">
<item name="android:background">@drawable/btn_cab_done_customab</item>
</style>

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

最佳答案

将此项目添加到与 pre-Honeycomb 相关的自定义主题中会出现错误,因为该属性在旧设备上的 Android 框架中不存在。

<item name="android:homeAsUpIndicator">@drawable/action_up_caret</item>

如果您使用的是 AppCompat 库,您应该覆盖自定义主题中的“homeAsUpIndicator”属性,而不是“android:homeAsUpIndicator”。

<item name="homeAsUpIndicator">@drawable/action_up_caret</item>

注意:如果您有兴趣,可以在此处查看 AppCompat 源代码。 https://github.com/android/platform_frameworks_support/tree/master/v7/appcompathttps://android.googlesource.com/platform/frameworks/support/+/master/v7/appcompat/

这里是我的测试项目的一些文件:

值\styles_example.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="Theme.Example" parent="@style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarItemBackground">@drawable/selectable_background_example</item>
<item name="popupMenuStyle">@style/PopupMenu.Example</item>
<item name="dropDownListViewStyle">@style/DropDownListView.Example</item>
<item name="actionBarTabStyle">@style/ActionBarTabStyle.Example</item>
<item name="actionDropDownStyle">@style/DropDownNav.Example</item>
<item name="actionBarStyle">@style/ActionBar.Solid.Example</item>
<item name="actionModeBackground">@drawable/cab_background_top_example</item>
<item name="actionModeSplitBackground">@drawable/cab_background_bottom_example</item>
<item name="actionModeCloseButtonStyle">@style/ActionButton.CloseMode.Example</item>

<!-- Light.DarkActionBar specific -->
<item name="actionBarWidgetTheme">@style/Theme.Example.Widget</item>

<item name="homeAsUpIndicator">@drawable/ic_launcher</item>
</style>

<style name="ActionBar.Solid.Example" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="background">@drawable/ab_solid_example</item>
<item name="backgroundStacked">@drawable/ab_stacked_solid_example</item>
<item name="backgroundSplit">@drawable/ab_bottom_solid_example</item>
<item name="progressBarStyle">@style/ProgressBar.Example</item>
</style>

<style name="ActionBar.Transparent.Example" parent="@style/Widget.AppCompat.ActionBar">
<item name="background">@drawable/ab_transparent_example</item>
<item name="progressBarStyle">@style/ProgressBar.Example</item>
</style>

<style name="PopupMenu.Example" parent="@style/Widget.AppCompat.PopupMenu">
<item name="android:popupBackground">@drawable/menu_dropdown_panel_example</item>
</style>

<style name="DropDownListView.Example" parent="@style/Widget.AppCompat.ListView.DropDown">
<item name="android:listSelector">@drawable/selectable_background_example</item>
</style>

<style name="ActionBarTabStyle.Example" parent="@style/Widget.AppCompat.ActionBar.TabView">
<item name="android:background">@drawable/tab_indicator_ab_example</item>
</style>

<style name="DropDownNav.Example" parent="@style/Widget.AppCompat.Spinner.DropDown.ActionBar">
<item name="android:background">@drawable/spinner_background_ab_example</item>
<item name="android:popupBackground">@drawable/menu_dropdown_panel_example</item>
<item name="android:dropDownSelector">@drawable/selectable_background_example</item>
</style>

<style name="ProgressBar.Example" parent="@style/Widget.AppCompat.ProgressBar.Horizontal">
<item name="android:progressDrawable">@drawable/progress_horizontal_example</item>
</style>

<style name="ActionButton.CloseMode.Example" parent="@style/Widget.AppCompat.ActionButton.CloseMode">
<item name="android:background">@drawable/btn_cab_done_example</item>
</style>

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

</resources>

values-v14\styles_example.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="Theme.Example" parent="@style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarItemBackground">@drawable/selectable_background_example</item>
<item name="android:popupMenuStyle">@style/PopupMenu.Example</item>
<item name="android:dropDownListViewStyle">@style/DropDownListView.Example</item>
<item name="android:actionBarTabStyle">@style/ActionBarTabStyle.Example</item>
<item name="android:actionDropDownStyle">@style/DropDownNav.Example</item>
<item name="android:actionBarStyle">@style/ActionBar.Solid.Example</item>
<item name="android:actionModeBackground">@drawable/cab_background_top_example</item>
<item name="android:actionModeSplitBackground">@drawable/cab_background_bottom_example</item>
<item name="android:actionModeCloseButtonStyle">@style/ActionButton.CloseMode.Example</item>

<!-- Light.DarkActionBar specific -->
<item name="android:actionBarWidgetTheme">@style/Theme.Example.Widget</item>

<item name="android:homeAsUpIndicator">@drawable/ic_launcher</item>
</style>

<style name="ActionBar.Solid.Example" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">@drawable/ab_solid_example</item>
<item name="android:backgroundStacked">@drawable/ab_stacked_solid_example</item>
<item name="android:backgroundSplit">@drawable/ab_bottom_solid_example</item>
<item name="android:progressBarStyle">@style/ProgressBar.Example</item>
</style>

<style name="ActionBar.Transparent.Example" parent="@style/Widget.AppCompat.ActionBar">
<item name="android:background">@drawable/ab_transparent_example</item>
<item name="android:progressBarStyle">@style/ProgressBar.Example</item>
</style>

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

</resources>

这样应该可以得到类似this的样子在你的 Activity 中。

关于android - 在android中自定义按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18373226/

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