[英]How to change color of floating action button on theme change?
我想在我的应用程序中应用不同的主题。 我想更改导航栏和浮动操作按钮的颜色。
现在我可以更改导航栏的颜色,但浮动操作按钮的颜色不会改变。 主题更改时,如何动态更改 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>
谢谢..
浮动操作按钮的颜色基于您的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'
}
谢谢你的回答。 但我只是在 fab 选择器中进行了更改。
<item android:state_pressed="true">
<shape android:shape="oval">
<solid android:color="?attr/colorAccent" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="oval">
<solid android:color="?attr/colorAccent" />
</shape>
</item>
<item>
<shape android:shape="oval">
<solid android:color="?attr/colorAccent" />
</shape>
</item>
这工作..
请使用带有浮动按钮的材质形状主题,现在非常简单,无需自定义
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.