I would like to know is there a CSS like way to style child Views within a particular Layout.
For example,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp">
<LinearLayout
android:id="@+id/fieldGroup1"
style="@style/field_group"
android:background="@drawable/rounded_border"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="10dp" >
<TextView
android:id="@+id/itemName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/itemExpiry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
<!-- More TextViews !>
</LinearLayout>
<!-- Another LinearLayout !>
</LinearLayout>
And the styles
<style name="field_group">
<item name="android:textViewStyle">@style/padded_text_view</item>
</style>
<style name="padded_text_view">
<item name="android:padding">5dp</item>
</style>
However, the TextViews within the LinearLayout that has the field_group style, don’t inherit the 5dp padding.
I know I can apply the style to every TextViews, but is there a better way to achieve this?
It seems to me that your style will not be applied to the childs. The explanation can be the following. Different views have the same attributes and can nest each other. Thus, if parent and child view has the same parameter that is overrode in style then, according to your logic, it will be applied to the parent and to the child. If you want for child other value you should create another style. It seems to be very consuming.
I guess that you can solve your problem using themes. Themes are applied to all elements of an activity. Thus, this can help you.