0

How do I avoid duplicate style items in the below example?

I have textSize 30sp with the style - TextStyle.

<style name="TextStyle">
     <item name="android:textSize">30sp</item>
</style>

The same textSize 30sp I am using in the below style. Is there any method apply the textsize- without duplicate writing of the style?

<style name="bottomText">
    <item name="android:textSize">30sp</item>
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">150dp</item>
</style>
Robert MacLean
  • 38,615
  • 24
  • 98
  • 151
Pavan Kumar
  • 1,566
  • 5
  • 23
  • 37

4 Answers4

1

Just let one Style inherit from the other:

 <style name="bottomText" parent="TextStyle">
      <item name="android:layout_width">fill_parent</item>
      <item name="android:layout_height">150dp</item>
  </style>

the style "bottomText" then has all attributes defined in "TextStyle", but can still be overwritten within bottomText.

danijoo
  • 2,743
  • 4
  • 21
  • 42
1
<style name="TextStyle">
    <item name="android:textSize">30sp</item>
</style>

<style name="bottomText" parent="@style/TextStyle">
  <item name="android:layout_width">fill_parent</item>
  <item name="android:layout_height">150dp</item>
</style>

<style name="boldText" parent="@style/bottomText">
  <item name="android:textStyle">bold</item>
</style>

Another approach:

<style name="bottomText.BoldText">
  <item name="android:textStyle">bold</item>
</style>

In last two cases boldText inherits from bottomText which defines android:layout_width and android:layout_height

mmBs
  • 8,126
  • 6
  • 40
  • 44
0
<style name="TextStyle">
        <item name="android:textSize">30sp</item>
   </style>

<style name="bottomText" parent="@style/TextStyle">
      <item name="android:layout_width">fill_parent</item>
      <item name="android:layout_height">150dp</item>
  </style>

let style of bootomText Inheritance the TextStyle

http://developer.android.com/guide/topics/ui/themes.html#DefiningStyles

Mejonzhan
  • 2,338
  • 1
  • 19
  • 27
  • text bold?? Is there bold attr on TextView in android? if you just want to set text attr, you can see this post http://stackoverflow.com/questions/3297437/shadow-effect-for-a-text-in-android/3297562#3297562 – Mejonzhan May 13 '13 at 13:33
  • Of course, there is a `bold` value for TextView's `android:textStyle` attribute. – mmBs May 13 '13 at 13:47
0

From what I can see, you don't need that second style since you need to set a layout_width and layout_height in the xml. You can just set that height and width when you create your layout and just use the style="@styles/TextStyle". If there's a reason you can't do it this way, please explain the problem a little better

codeMagic
  • 44,229
  • 13
  • 74
  • 91