如何创建自定义样式?

How to create a custom style?

我的 axml 文件中有几个视图小部件,它们看起来像这样:

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

我不想重复代码,所以我在 Values 文件夹中创建了 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="marginBottom">5dp</item>
       <item name="android:background">#c0c0c0</item>
  </style>
</resources>

现在我正尝试在我的视图中引用样式:

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

但是没用。我已经在互联网上尝试了所有来创造风格。我哪里错了?

Here is about how to use style in Xamarin.Android

将您的代码替换为:

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>编译器无法识别

layout.xml:

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

android:style=替换为style=,在link.

中可以看到