proffstack
proffstack

Reputation: 45

How to create a custom style?

I have several View widgets in my axml file they looks like that:

 <View
     android:layout_width="fill_parent"
     android:layout_height="2dp"
     android:layout_marginTop="1dp"
     android:layout_marginBottom="5dp"
     android:background="#c0c0c0" />

I don't want to repeat the code so I created styles.xml file in Values folder with the following content:

<resources>
  <style name="HorizontalLine">
       <item name="android:layout_width">fill_parent</item>
       <item name="android:layout_height">2dp</item>
       <item name="android:layout_marginTop">1dp</item>
       <item name="marginBottom">5dp</item>
       <item name="android:background">#c0c0c0</item>
  </style>
</resources>

Now I'm trying to reference the style in my Views like that:

<View
     android:style="@style/HorizontalLine"
/>

But it doesn't work. I've tried all in internet to create style. Where I'm wrong?

Upvotes: 0

Views: 130

Answers (1)

Robbit
Robbit

Reputation: 4358

Here is about how to use style in Xamarin.Android

Replace your code with this:

styles.xml:

<resources>
  <style name="HorizontalLine">
       <item name="android:layout_width">fill_parent</item>
       <item name="android:layout_height">2dp</item>
       <item name="android:layout_marginTop">1dp</item>
       <item name="android:layout_marginBottom">5dp</item>
       <item name="android:background">#c0c0c0</item>
  </style>
</resources>

<item name="marginBottom">5dp</item> it can not be recognized by the compiler.

layout.xml:

<View
     style="@style/HorizontalLine"
/>

Replace android:style= with style=, you can see it in the link.

Upvotes: 1

Related Questions