I want to apply different themes to my application. I want to change the color of navigation bar and floating action button.
Now I am able to change color of navigation bar , but floating action button's color dose not get change. How can I change color of fab dynamically when theme is changed?
Style:
<!-- 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>
Fab :
<?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>
Fab selector :
<?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>
Thank you..
The floating action button is colored based on your colorAccent
<style name="AppTheme" parent="Base.Theme.AppCompat.Light">
<item name="colorAccent">@color/accent</item>
</style>
Or from code:
fab.setBackgroundTintList(getResources().getColorStateList(R.color.blue));
Or from XML:
Change attribute of app:backgroundTint
But it seems that you are using ImageButton not FloatingActionButton , You should use:
<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" />
And in the dependencies add:
dependencies {
compile 'com.android.support:design:23.1.1'
}
Thank you for answers. But I just changed in fab selector.
<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>
This worked..
请使用带有浮动按钮的材质形状主题,现在非常简单,无需自定义
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.