简体   繁体   中英

Why is my actionbar not showing and how do I fix it?

Ok so I'm developing an Android app using Android Studio and I'm having real trouble getting the action bar to display. From the code, I cannot see why the action bar doesn't show. Can anyone spot what might be causing it?

Main Activity

public class HomeActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

    }

Main activty xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".HomeActivity"
   >

<ListView
    android:id="@+id/apps_list"
    android:layout_width="match_parent"
    android:layout_height="420dp"
    android:divider="@color/divider"
    android:layout_alignParentBottom="true"
    >
</ListView>

My Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.easylauncher" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".HomeActivity"
            android:label="@string/app_name"
            android:launchMode="singleTask"
            android:stateNotNeeded="true" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.HOME" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Styles xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="AppTheme.Base"/>

    <style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->

        <!-- colorPrimary is used for the default action bar background -->
        <item name="colorPrimary">@color/primary</item>

        <!-- colorPrimaryDark is used for the status bar -->
        <item name="colorPrimaryDark">@color/primary_dark</item>



        <item name="android:textColorPrimary">@color/primary_text</item>



        <item name="android:icon">@color/icons</item>

        <item name="android:divider">@color/divider</item>

        <!-- colorAccent is used as the default value for colorControlActivated
             which is used to tint widgets -->
        <item name="colorAccent">@color/accent</item>

        <!-- You can also set colorControlNormal, colorControlActivated
             colorControlHighlight & colorSwitchThumbNormal. -->
    </style>

</resources>

Extend your Activity with ActionBarActivity

public class HomeActivity extends ActionBarActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

    }

1- make changes in your Manifest file,

 <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />

2- download, android-support-v7-appcompat

3- Add downloaded library to your project

在此处输入图片说明

在此处输入图片说明

and extends Activity instead of ActionBarActivity.

hope it will help.

只是扩展了ActionBarActivity而不是Activity

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM