gpt4 book ai didi

java - 如何在主题更改时更改 float 操作按钮的颜色?

转载 作者:可可西里 更新时间:2023-11-01 11:43:02 27 4
gpt4 key购买 nike

我想为我的应用程序应用不同的主题。我想更改导航栏和 float 操作按钮的颜色。

现在我可以更改导航栏的颜色,但 float 操作按钮的颜色不会改变。如何在主题更改时动态更改 fab 的颜色?

风格:

    <!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="colorAccent_500">@color/accent_500</item>
<item name="colorAccent_700">@color/accent_700</item>

<item name="windowActionBarOverlay">false</item>

<item name="windowActionBar">false</item>

<item name="windowNoTitle">true</item>
<item name="android:windowBackground">@color/background_material_light</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />


<style name="BlueTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#3F51B5</item>
<item name="colorPrimaryDark">#303F9F</item>
<item name="colorAccent">#03A9F4</item>

<item name="windowActionBarOverlay">false</item>

<item name="windowActionBar">false</item>

<item name="windowNoTitle">true</item>
<item name="android:windowBackground">@color/background_material_light</item>
</style>


<style name="AppTheme.Solarized">
<item name="colorPrimary">@color/orange300</item>
<item name="colorPrimaryDark">@color/orange500</item>
<item name="colorAccent">@color/solarizedOrange</item>
<item name="android:textColorPrimary">@color/solarizedBase01</item>
<item name="android:textColorSecondary">@color/solarizedBase1</item>
</style>


</resources>

工厂:

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_fab"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="bottom|end">

<View
android:id="@+id/myfab_shadow"
android:layout_width="72dp"
android:layout_height="72dp"
android:layout_gravity="center"
android:background="@drawable/fab_shadow"
android:focusable="false" />

<ImageButton
android:id="@+id/imgbtn_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/fab_selector"
android:cropToPadding="true"
android:padding="10dp"
android:src="@drawable/ic_add_white_36dp"
android:stateListAnimator="@animator/fab_elevation_selector" />

</FrameLayout>

工厂选择器:

  <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true">
<shape android:shape="oval">
<solid android:color="@color/colorAccent" />
</shape>
</item>

<item android:state_focused="true">
<shape android:shape="oval">
<solid android:color="@color/accent_700" />
</shape>
</item>

<item>
<shape android:shape="oval">
<solid android:color="@color/accent_500" />
</shape>
</item>

</selector>

谢谢你..

最佳答案

float 操作按钮的颜色基于您的colorAccent

<style name="AppTheme" parent="Base.Theme.AppCompat.Light">
<item name="colorAccent">@color/accent</item>
</style>

或来自代码:

fab.setBackgroundTintList(getResources().getColorStateList(R.color.blue));

或来自 XML:

改变app:backgroundTint的属性

但您似乎使用的是 ImageButton 而不是 FloatingActionButton,您应该使用:

 <android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin" />

并在依赖项中添加:

dependencies {
compile 'com.android.support:design:23.1.1'
}

关于java - 如何在主题更改时更改 float 操作按钮的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35424642/

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