styles.xml无法正常工作

时间:2018-08-07 06:36:21

标签: c# android xamarin xamarin.android

上下文:

我最近开始尝试学习如何开发非常简单的Android应用程序,并且一直在关注许多在线提供的教程和资源。我正在看一个练习,该练习显示了如何使用其他xml文件,例如styles.xml将某种样式应用于某项内容,而不必将相同的代码复制并粘贴到main.axml中多次。因此,例如,如果您有许多按钮,则可以对所有这些按钮应用一种通用样式。

问题: 我创建了一个不执行任何操作的应用程序,我只想尝试对布局进行梳理并感觉一下。它具有4个按钮(尚未单击)。这就是它的外观,后跟main.axml代码。

android_ui_1

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <TextView
            android:id="@+id/calculator_text_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:padding="10dp"
            android:textSize="50sp"
            android:text="0" />
    </HorizontalScrollView>
    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="4"
        android:columnCount="2"
        android:rowCount="3">
        <Button
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:background="#ff3f51b5"
            android:textSize="25sp"
            android:text="1st"
            android:onClick="ButtonClick" />
        <Button
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:background="#F44336"
            android:textSize="25sp"
            android:text="2nd"
            android:onClick="ButtonClick" />
        <Button
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:background="#ff9c27b0"
            android:textSize="25sp"
            android:text="3rd"
            android:onClick="ButtonClick" />
        <Button
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:background="#ff4caf50"
            android:textSize="25sp"
            android:text="4th"
            android:onClick="ButtonClick" />
    </GridLayout>
</LinearLayout>

现在,我尝试对按钮应用“样式”,因为它们具有许多相同的属性,这将使代码更简单易读。这就是我应用样式的方式:

<Button
   style="@style/button_style"
   android:background="#ff3f51b5"
   android:text="1st"/>
<Button
   style="@style/button_style"
   android:background="#F44336"
   android:text="2nd"/>
<Button
   style="@style/button_style"
   android:background="#ff9c27b0"
   android:text="3rd"/>
<Button
   style="@style/button_style"
   android:background="#ff4caf50"
   android:text="4th"/>

这就是styles.xml中的内容:

  <!-- Base application theme. -->
  <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
  </style>

  <style name="button_style">
    <item name="android:layout_width">0dp</item>
    <item name="android:layout_height">0dp</item>
    <item name="android:layout_rowWeight">1</item>
    <item name="android:layout_columnWeight">1</item>
    <item name="android:textSize">25dp</item>
    <item name="android:onClick">ButtonClick</item>
  </style>
</resources>

最后是用户界面的外观。显而易见,styles的按钮属性没有被保留:

2nd

我似乎无法弄清楚我在做什么错。我知道我可能永远都不需要使用按钮来执行此操作,但是我想这可能对以后的其他操作有所帮助。

1 个答案:

答案 0 :(得分:1)

使您的 button_style 扩展为

 parent="Widget.AppCompat.Button"

完整样式为

<style name="button_style" parent="Widget.AppCompat.Button">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">0dp</item>
<item name="android:layout_rowWeight">1</item>
<item name="android:layout_columnWeight">1</item>
<item name="android:textSize">25dp</item>

</style>