milus
milus

Reputation: 128

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.

Upvotes: 5

Views: 3350

Answers (1)

Htut
Htut

Reputation: 265

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>

Upvotes: 5

Related Questions