【问题标题】:How I can do a button with left/right side as rounded as circle我如何做一个左/右侧像圆形一样圆的按钮
【发布时间】:2017-06-19 11:47:18
【问题描述】:

我正在尝试做一个侧面圆角的按钮,但是当我改变屏幕的大小时,按钮侧面左侧被圆角为圆形,不保持纵横比(我认为)。

我想在每个尺寸都有下一个切换按钮。如何为每个分辨率制作一个边沿四舍五入的 XML?

我已阅读此内容,但不是解决方案 (How to make the corners of a button round?)

【问题讨论】:

  • 检查我的回答...

标签: android rounded-corners aspect-ratio screen-size


【解决方案1】:

在 xml 中创建一个可绘制的资源文件。以下是蓝色按钮的代码

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="#2196f3"></solid>

    <corners android:radius="12dp"></corners>
</shape>

然后只需将此资源文件指定为您的其中一个 textView 的背景

希望对你有帮助

【讨论】:

  • 如果我有一个具有该值的大屏幕,则按钮的侧面可能不会完全变圆。
  • 只需将角的半径更改为更小的尺寸。
【解决方案2】:

其他答案是正确的......几乎: 如果您将角设置为某个值(例如 20dp),但您的按钮高度会更大(例如 40dp) - 您将以圆角而不是圆边结束。因此解决方案很简单:将android:corners 属性设置为某个足够高的值,该值将大于按钮高度:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/colorPrimary" />
    <corners android:radius="200dp" />
    <size
        android:height="16dp"
        android:width="32dp" />
</shape>

【讨论】:

    【解决方案3】:
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
        <corners android:radius="20dp" />
        <solid android:color="#1ee" />
    </shape>
    

    【讨论】:

    • 谢谢,但与我在下面其他答案中解释的问题相同
    • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
    猜你喜欢
    • 1970-01-01
    • 2018-03-28
    • 2019-02-11
    • 2017-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-23
    相关资源
    最近更新 更多