【问题标题】:Are there some default bordered backgrounds?是否有一些默认的边框背景?
【发布时间】:2012-05-18 08:59:34
【问题描述】:

我想绘制带有凸起斜角边框的自定义面板状视图(Swing 术语)。有一些预定义的方法吗?例如,是否可以从当前按钮样式中获取边框?还是我必须自己创建 9 补丁之类的?

【问题讨论】:

  • 我认为您创建了自己的九个补丁图像
  • 如果你想要的只是一个斜角边框,你可以用图形绘图来做到这一点,但我自己还没有尝试过。

标签: android drawing border bevelled


【解决方案1】:

一种粗略的方法是制作一个可绘制的 XML,其中包含一个图层列表,然后自己编写按钮的结构。在我的示例中,您实现了以下按钮样式。

如果需要,所有颜色都可以更改以适合您的风格。我知道拐角应该是 45º 而不是 90º,但是如果没有一些测试,我没有简单的方法在这里展示这个。源 XML 如下:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- The gray part of the border -->
    <item>
        <shape>
            <solid android:color="#fccc"></solid>
        </shape>
    </item>
    <!-- The black part of the border -->
    <item android:right="2dip"
          android:bottom="2dp">
        <shape>
            <solid android:color="#f000"></solid>
        </shape>
    </item>
    <!-- The content -->
    <item android:left="2dip"
          android:top="2dp"
          android:right="2dip"
          android:bottom="2dp"> 
        <shape>
            <solid android:color="#ffafafaf"></solid>
        </shape> 
    </item>
</layer-list>

作为您可以在此处实现的进一步示例,只需将&lt;corners android:radius="4dp"&gt;&lt;/corners&gt;添加到边框形状,我们就可以获得稍微不那么刺眼的样式:

或者您可以采用不同的方法并避免凸起的斜角边框,使其在 XML 方面更容易并且可能更好。要实现这种按钮:

XML 就像下面这样简单:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="4dp"></corners>
    <solid android:color="#fccc"></solid>
    <stroke android:width="2dp" android:color="#ff6f6f6f"></stroke>
</shape>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-18
    • 1970-01-01
    • 2013-01-08
    • 1970-01-01
    • 2023-01-22
    • 1970-01-01
    相关资源
    最近更新 更多