我想在我的应用程序中应用不同的主题。我想改变导航栏和浮动动作按钮的颜色。
现在我可以改变导航栏的颜色,但浮动动作按钮的颜色不会改变。当主题改变时,如何动态改变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>
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选择器:
<?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>
谢谢..
答案 0 :(得分:2)
浮动操作按钮根据您的 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'
}
答案 1 :(得分:0)
谢谢你的回答。但我刚刚换过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>
这有效..
答案 2 :(得分:0)
请使用带有浮动按钮的材料形状主题,它现在非常容易,无需自定义