有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android Actionbar不应用自定义图像背景

我使用的是Android Studio,我尝试将自定义图像添加到操作栏背景中,现在我在youtube上使用了一些教程,其中说我应该将以下代码添加到我的styles.xml

 <style name="custom_actionbar" parent="@安卓:style/Theme.Holo.Light.DarkActionBar">
        <item name="安卓:actionBarStyle">@style/custom_style</item>
    </style>
    <style name="custom_style" parent="@安卓:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
        <item name="安卓:background">@drawable/bar</item>
    </style>

并显示。xml

<activity
       安卓:name=".MainActivity"
       安卓:label="@string/app_name"
       安卓:screenOrientation="portrait"
       安卓:theme="@style/custom_actionbar">

现在,它应该可以工作了,尽管它显示了以下错误:

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity

所以我在互联网上查了一下,发现了this,尽管在使用了解决方案之后,应用程序仍然会因为同样的错误而崩溃(在使用解决方案之后,我已经用调试检查了几次错误)

如何使用主题。AppCompact主题是否正确,应用程序能否正常工作

我的全部舱单。xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    package="com.none.myapplication">

    <application
        安卓:allowBackup="true"
        安卓:icon="@mipmap/ic_launcher"
        安卓:label="@string/app_name"
        安卓:supportsRtl="true"
        安卓:theme="@style/AppTheme">
        <activity
            安卓:name=".MainActivity"
            安卓:label="@string/app_name"
            安卓:screenOrientation="portrait"
            安卓:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action 安卓:name="安卓.intent.action.MAIN" />

                <category 安卓:name="安卓.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            安卓:name=".Food"
            安卓:label="@string/title_activity_food"
            安卓:screenOrientation="portrait"
            安卓:theme="@style/AppTheme.NoActionBar" />
        <activity
            安卓:name=".Time"
            安卓:label="@string/title_activity_time"
            安卓:screenOrientation="portrait"
            安卓:theme="@style/AppTheme.NoActionBar" />
        <activity
            安卓:name=".Info"
            安卓:screenOrientation="portrait" />
        <activity
            安卓:name=".Training"
            安卓:label="@string/title_activity_training"
            安卓:theme="@style/AppTheme.NoActionBar" />
        <activity 安卓:name=".firstEntry"></activity>
    </application>

风格。xml

<resources>

    <!-- 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>
    </style>

    <style name="custom_actionbar" parent="@style/Theme.AppCompat.Light">
        <item name="安卓:actionBarStyle">@style/custom_style</item>
    </style>

    <style name="custom_style" parent="@style/Widget.AppCompat.Light.ActionBar">
        <item name="安卓:background">@drawable/bar</item>
    </style>

    <style name="LightDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="安卓:textColor">@安卓:color/primary_text_light</item>
        <item name="background">@drawable/bar</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

共 (1) 个答案

  1. # 1 楼答案

    尝试使用AppCompatTheme

    在res/值/样式中。xml:将您的样式替换为

     <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!  Customize your theme here.  >
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    
    <style name="custom_actionbar" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/custom_style</item>
    </style>
    
    <style name="custom_style" parent="@style/Widget.AppCompat.Light.ActionBar">
        <item name="android:background">@drawable/bar</item>
    </style>
    

    现在,在清单中,使用以下命令在活动中添加自定义操作栏主题:

     <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@style/custom_actionbar">
    

    编辑

    在主活动中,修改:

     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        //setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
    
        prefs = getSharedPreferences("com.none.myapplication", MODE_PRIVATE);
    
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
    

    替代解决方案

    将图像添加到toolbar背景中:

    <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@drawable/bar"  //your image
            app:popupTheme="@style/AppTheme.PopupOverlay" />
    
    </android.support.v7.widget.Toolbar>
    

    还要修改清单中的主题:使用toolbar而不是ActionBar

    <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar">