【问题标题】:Android: Round button corners without changing the background colorsAndroid:圆形按钮角而不改变背景颜色
【发布时间】:2017-10-06 03:17:43
【问题描述】:

好的,所以我知道如何在 android 中使用可绘制背景来圆角。问题是,这也需要我手动设置按钮的颜色。如何在保持按钮主题颜色的同时更改圆角半径?

round_button.xml

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="40dp" />
    <solid android:color="#80000000" />
</shape>

在activity_main.xml中

<Button
    android:id="@+id/button2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_column="1"
    android:layout_columnSpan="1"
    android:layout_columnWeight="1"
    android:layout_row="0"
    android:background="@drawable/round_button"
    android:text="Button" />

我只是希望主题颜色仍然应用于按钮,但也能够更圆角。

【问题讨论】:

  • 你能用一个例子或一些代码描述你的问题吗?
  • @ANUJGUPTA 我添加了一些代码

标签: android android-drawable android-button android-styles


【解决方案1】:

尝试在需要设置颜色的地方将按钮的颜色设置为透明,这样可以保持主题颜色。

【讨论】:

  • 这只是将按钮颜色设置为黑色
【解决方案2】:

我设法通过使用此按钮获取按钮的典型颜色来解决此问题:

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="40dp" />
    <solid android:color="?android:colorButtonNormal" />
</shape>

【讨论】:

    【解决方案3】:

    将色调应用于所需颜色的按钮。

    button.setColorFilter(ContextCompat.getColor(context, R.color.COLOR_YOUR_COLOR), android.graphics.PorterDuff.Mode.REQUIRED_MODE);
    

    【讨论】:

      猜你喜欢
      • 2014-11-05
      • 2019-10-27
      • 2015-10-25
      • 1970-01-01
      • 2015-09-09
      • 2015-10-29
      • 1970-01-01
      • 1970-01-01
      • 2011-12-30
      相关资源
      最近更新 更多