gpt4 book ai didi

Android可绘制: Specifying shape width in percent in the XML file?

转载 作者:IT老高 更新时间:2023-10-28 22:18:05 29 4
gpt4 key购买 nike

我正在尝试创建一个简单的 Drawable,我想将其设置为 View 的背景(使用 setBackgroundDrawable)。我只是想将drawable的背景分成2个相等的矩形(50% - 50%),第一个要填充黑色,第二个填充白色:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/back_color_1">
<shape android:shape="rectangle">
<solid android:color="#000000" />
</shape>
</item>
<item android:id="@+id/back_color_2">
<shape android:shape="rectangle">
<solid android:color="#FFFFFF" />
</shape>
</item>
</layer-list>

如何在可绘制 XML 定义文件中指定每个形状的宽度应为 50%?像 android:width="50%".

(我正在开发 Android 3.0,但我认为这是一个普遍的 Android 问题。)

P.S:您可以在 CSS 或 XAML 中执行此操作。

最佳答案

您不能指定百分比。您需要为其指定一个维度值。

android:width 在这里定义:http://developer.android.com/reference/android/R.attr.html#width :

(强调我的)

Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

有关每种维度类型的定义,请参阅:http://developer.android.com/guide/topics/resources/more-resources.html#Dimension

您需要创建自己的属性(参见 styleable)并在 onDraw(...) 中自己执行计算。

请参阅这两个问题及其中的链接作为示例:

关于Android可绘制: Specifying shape width in percent in the XML file?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6061387/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com