Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inherit style in layout in Android

I've tried to inherit styles in Android layout xml. Now it looks like:

layout.xml:

<RelativeLayout
    android:id="@id/btn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    style="@style/AppTheme.ButtonLayout">

    <TextView
        android:id="@id/btn_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/AppTheme.ButtonLayout.TextView" />

</RelativeLayout>

styles.xml:

<style name="AppTheme.ButtonLayout">
    <item name="android:textViewStyle">@style/AppTheme.ButtonLayout.TextView</item>
</style>

<style name="AppTheme.ButtonLayout.TextView" parent="android:Widget.Holo.Light.TextView">
    <item name="android:textColor">@android:color/white</item>
</style>

I want to get rid of style attribute from the TextView, but it should inherit appearance from RelativeLayout. Is it possible?

Edit: I want this TextView to have such style only within RelativeLayout with @style/AppTheme.ButtonLayout style. When it is inside layout without this style, it should looks like default Android TextView.

like image 494
milus Avatar asked Sep 03 '14 08:09

milus


People also ask

How to inherit styles in Android app?

Styles can be inherited from one style to another style. We can inherit android in-built style into user style and can override property if required. Styles are used in view using attribute style . And at Activity and application level, we use attribute as android:theme . To use style, first create an XML file in res/values with arbitrary name.

What is the use of styles in Android?

Styles can be used at view level in layout as well as application and activity level in AndroidManifest.xml. Styles can be inherited from one style to another style. We can inherit android in-built style into user style and can override property if required. Styles are used in view using attribute style.

How to use style attribute in XML layout file in Android?

Once your style is defined, you can use it in your XML Layout file using style attribute as follows − To understand the concept related to Android Style, you can check Style Demo Example. Android supports style Inheritance in very much similar way as cascading style sheet in web design.

How to make a style inherit from another style in WordPress?

Step 1) So first go to the style.xml file into your previously created styles. Step 2) Now for example if you want to make the style to inherit from another style then what you need to do is for example If we want to make the style as a parent style we need to add the attribute called ' Parent '. And then we need to give the name of this attribute.


1 Answers

you can inherit styles in Android layout xml from style.xml example at following..

AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
       <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.prj.name"
       android:versionCode="1"
        android:versionName="1.0" >

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

  <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

  <activity> </activity>
  </application>
</manifest>

themes_apptheme.xml you can call declare name from parent name

 <?xml version="1.0" encoding="utf-8"?>
 <!-- Generated with http://android-holo-colors.com -->
<resources xmlns:android="http://schemas.android.com/apk/res/android">

<style name="AppTheme" parent="@style/_AppTheme" />

<style name="_AppTheme" parent="android:Theme.Holo.Light">

    <item name="android:textColorHighlight">#99ff6d00</item>
       <item name="android:textViewStyle">@style/TextViewAppTheme</item>
</style>

styles_apptheme.xml //what you want to design you can call from drawable folder with png format

 <style name="TextViewAppTheme" parent="android:Widget.Holo.Light.TextView">
    <item name="android:background">@drawable/apptheme_text_holo_light</item>
</style>

style.xml

<style name="Widget.Holo.Light.TextView" parent="Widget.TextView">
    <item name="android:textcolor"> .........</item>
</style>

 <style name="Widget.TextView">
    <item name="android:textAppearance">?android:attr/textAppearanceMediumInverse</item>
    <item name="android:gravity">center_vertical</item>
</style>
like image 77
Joseph Avatar answered Oct 11 '22 22:10

Joseph