我怎么能改变ActionBar颜色?我已经实现了这个并且它可以工作但不是我想要的.它只在活动开始后才会改变颜色,因此在应用程序启动后,会显示原始条形颜色,只有一秒钟后我的自定义颜色会被显示.还有其他方法可以解决这个问题吗?提前致谢
....
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar;
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#879f38")));
.....
Run Code Online (Sandbox Code Playgroud) 我需要隐藏或更改我的活动操作栏的颜色。但是当我尝试这样做时出现错误FATAL EXCEPTION: main。
我的活动主题是android:theme="@style/Theme.AppCompat.Light.Dialog
隐藏
getSupportActionBar().hide();
Run Code Online (Sandbox Code Playgroud)
更改背景颜色
android.app.ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.RED));
Run Code Online (Sandbox Code Playgroud)
我被这个错误困住了。
错误日志
10-03 18:09:38.088 14160-14160/com.example.myown.myapplication
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myown.myapplication, PID: 14160
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myown.myapplication/com.example.myown.myapplication.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
Run Code Online (Sandbox Code Playgroud) 我试图改变背景颜色的ActionBar我的应用程序.
我尝试了以下更改,
在style.xml文件中,我添加了"MyTheme"和"ActionBarBackground",如下所示.
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<style name="MyTheme" parent="AppBaseTheme"> …Run Code Online (Sandbox Code Playgroud) 我正在主要活动中设置工具栏,并尝试使用不同的片段更改背景颜色。因此,基本上,我试图访问片段内的工具栏对象并设置不同的背景色。我尝试做的几件事是:
访问工具栏,如:(((ActionBarActivity)getActivity())。getSupportActionBar()。setBackgroundColor(XXX);
但是我无法访问片段内的setBackgroundColor函数。它在Main Activity中完美运行。
说明:我想在工具栏上应用渐变.我已经做了.我在style.xml中的应用程序主题上应用了这个渐变.
这是我的style.xml
<resources>
<style name="MyMaterialTheme" parent="MyMaterialTheme.Base">
</style>
<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">@drawable/toolbar_gradient</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="MyMaterialTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="MyMaterialTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="MyTabLayoutTextAppearance" parent="TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse">
<item name="android:textSize">16sp</item>
<item name="android:textAllCaps">true</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
这是我的gradient.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:type="linear"
android:startColor="@color/upper"
android:endColor="@color/bottom"
android:angle="270"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
这是我的color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="main_background">#FFFFFF</color>
<!--009788 26A69A-->
<color name="colorPrimary">#009788</color>
<color name="colorPrimaryDark">#E53935</color><!--00695C-->
<color name="dull_color">#26A69A</color>
<color name="colorAccent">#3F51B5</color>
<color name="textColorPrimary">#FFFFFF</color>`
<color name="txtColor">#000000</color>
<color name="windowBackground">#FFFFFF</color>
<color name="navigationBarColor">#000000</color>
<color name="counter_text_bg">#f32f32</color>
<color name="list_background">#FFFFFF</color> …Run Code Online (Sandbox Code Playgroud)