How can I achieve this theme with AppCompat?

ⅰ亾dé卋堺 提交于 2019-12-12 04:25:49

问题


I am trying to create this theme for one, and just one of my activities:

Since I am learning how this styles work, and I am now beginning to be aware that we have more different & incompatible ways of styling in android than atoms in the whole universe. So I've tried to learn from this site: http://jgilfelt.github.io/android-actionbarstylegenerator

However when I try to apply its theme in my project that is using AppCompat I get the

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

error type.

I am extending from ActionBarActivity in my tabbed activity and I don't feel safe going back now (someone suggested extending Activity to solve this issue), since in previous posts I was suggested to use AppCompat, and ActionBarActivity seems somehow to be AppCompat related.

I've spent all my week googling for solutions and I've came across these problems:

  • The official Android styling documentation is deprecated.
  • The code generated by Android Studio is also deprecated in method such as this.*
  • http://jgilfelt.github.io/android-actionbarstylegenerator is deprecated.



BUT, to the point now, this is what I have at the moment:

So, the only thing I am not being able to do is

  • putting the logo in the top left corner of the screen (in the action bar), in a way that only affects the action bar of this particular activity, (Done)

  • make the orange bar showing which tab is selected.

How can I achieve this in the most simple way?

This is my values/style.xml file:

<resources>

    <!-- Base application theme. -->
    <style name="SmartCampusTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/actionbar_bg_color</item>
    </style>

    <style name="GenericProgressIndicator" parent="@android:style/Widget.ProgressBar.Small">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:indeterminate">true</item>
    </style>

</resources>

My AndroidManifest.xml:

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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:logo="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/SmartCampusTheme">
        <!-- Splash screen while connecting to the web-server -->
        <activity
            android:name=".HomeScreen"
            android:label="@string/title_activity_home_screen"
            android:parentActivityName=".AbstractPortraitActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="greensmartcampus.eu.smartcampususerfeedbackapp.AbstractPortraitActivity" />
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- The activity where all actions take place -->
        <activity
            android:name=".ControlActivity"
            android:label="@string/title_activity_home_screen"
            android:parentActivityName=".AbstractPortraitActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="greensmartcampus.eu.smartcampususerfeedbackapp.AbstractPortraitActivity" />
        </activity>
    </application>

</manifest>

*Ps. it's funny how the most up-to-date Android Studio generates deprecated code for tabbed activities and then complains about it being deprecated as if it is your fault...

Important Note: I am a huge fan of MVC and a modularity priest (OSGi practitioner). So I would love if all these styling solutions could be all contained in the style and drawing XML files (as they should be if I correctly recall), the layout information is all contained in my layout files and my java files only contain code about behavior. For example I could just show the action bar icon with java through getSupportActionBar().setDisplayShowHomeEnabled(true), but inserting layout related stuff in java disrupts the MVC pattern. Also, the website that I mention above can create the layout with the icon in the actionbar without using a single java line. I just need to know how can that be done with AppCompat, which apparently is what I am using because other solutions are deprecated.

来源:https://stackoverflow.com/questions/28374240/how-can-i-achieve-this-theme-with-appcompat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!