【问题标题】:Custom background of button not showing up in Xamarin AndroidXamarin Android 中未显示按钮的自定义背景
【发布时间】:2021-10-17 10:33:57
【问题描述】:

我使用以下代码制作了一个自定义 .xml 文件:

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="false">
    <layer-list>
      <item>
        <shape>
          <stroke android:width="5dp" android:color="@color/colorAccent"/>
          <corners android:radius="0dp"/>
        </shape>
      </item>
    </layer-list>
  </item>

  <item android:state_pressed="true">
    <layer-list>
      <item>
        <shape>
          <stroke android:width="5dp" android:color="@color/colorAccent"/>
          <corners android:radius="0dp"/>
        </shape>
      </item>
    </layer-list>
  </item>
</selector>

在我的 activity_main.xml 我有以下按钮:

<Button
    android:background="@drawable/buttonname"
    android:textColor="@color/colorAccent"
    android:text="Next"
    android:layout_marginVertical="30dp"
    android:layout_gravity="center"
    android:layout_width="200dp"
    android:layout_height="50dp"/>

如果我将鼠标悬停在 @drawable/buttonname 上,我会看到我想要的设计,但我的按钮背景不是设计,而是我的样式文件夹中的 colorPrimary,无论是在 Visual Studio 的预览中还是在模拟时。我已经多次重新启动 Visual Studio,但无济于事。我是不是忘记了什么?有没有办法解决这个问题?

感谢任何帮助。

【问题讨论】:

  • 从您发布的xml 文件中,我们发现您将两种状态都设置为颜色colorAccent,这也与结果一致。但我还发现您将android:textColor 设置为colorAccent,这会使文本不可见,因为它的字体颜色与背景颜色相同。你想要什么样的效果?
  • 我想要一个除了边框和文字之外透明的按钮。

标签: android visual-studio xamarin


【解决方案1】:

如果我将 Button 更改为 TextView 并在我放入 android:gravity="center" 标记后具有所需的布局,则背景工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-28
    相关资源
    最近更新 更多