Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Do I just have one shot per property through the use of themes? Is there nothing like CSS in Android?

Here they write:

To apply a style definition as a theme, you must apply the style to an Activity or application in the Android manifest. When you do so, every View within the Activity or application will apply each property that it supports. For example, if you apply the CodeFont style from the previous examples to an Activity, then all View elements that support the text style properties will apply them.

So, when I set TextColor to Red, do ALL Elements with Text change to red?? Arent there powerful specifier like in CSS style-sheets? I just see the concept of styles (style="@style/CodeFont") practicable.

like image 471
OneWorld Avatar asked Sep 20 '10 12:09

OneWorld


2 Answers

In response to your comments you might find what your looking for with the property textAppearance.

Open the themes.xml file here:
https://android.googlesource.com/platform/frameworks/base/+/froyo-release/core/res/res/values/themes.xml

If you do a find for textAppearance you notice various entries such as:

<style name="Theme">
    <item name="textAppearanceButton">@android:style/TextAppearance.Widget.Button</item>
    ...
    <item name="panelTextAppearance">?android:attr/textAppearanceInverse</item>
    ...

This allows you to some extent to style Components throughout your application as shown in the Maragues example.

So whilst its not possible to fully control the style of all child elements withint say a listview, scroll view etc, there is some limited support for controlling the styles some Text based components such as the TextView and or Button.

like image 187
Emile Avatar answered Sep 27 '22 18:09

Emile


If you read just below the text you quoted:

To set a theme for all the activities of your application, open the AndroidManifest.xml file and edit the tag to include the android:theme attribute with the style name

If you want a theme applied to just one Activity in your application, then add the android:theme attribute to the tag instead.

And a little bit after that, it describes how to inherit and override properties, just like CSS:

If you like a theme, but want to tweak it, just add the theme as the parent of your custom theme. For example, you can modify the traditional dialog theme to use your own background image like this:

<style name="CustomTextView" parent="@android:style/Widget.TextView">
    <item name="android:background">@drawable/custom_background</item>
</style>

Then, to interit from CustomTextView

<style name="CustomTextView.RedText">
   <item name="android:textColor">@color/red</item> 
</style>

Once designing the XML layout, you can inherit a theme and override the properties

<TextView
    android:id="@+id/example"

    style="@style/CustomTextView.RedText"

    android:textColor="@color/green" />

I think you should read again the page you yourself linked, it's well explained :)

like image 20
Maragues Avatar answered Sep 27 '22 18:09

Maragues