Android Drawable:在 XML 文件中以百分比形式指定形状宽度?

问题描述 投票:0回答:6

我正在尝试创建一个简单的 Drawable,我想将其设置为视图的背景(使用 setBackgroundDrawable)。我只是想将可绘制的背景分成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 xml drawable shapes
6个回答
13
投票

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

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

(强调我的)

必须是维度值,是一个浮点数附加一个单位,如“14.5sp”。可用单位有:px(像素)、dp(与密度无关的像素)、sp(基于首选字体大小的缩放像素)、in(英寸)、mm(毫米)。

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

您需要创建自己的属性(请参阅styleable)并在

onDraw(...)
中自行执行计算。

请参阅这两个问题及其中的链接以获取示例:


5
投票

没有真正的百分比设置,但您可能会接近。

实现这一点的最简单方法似乎是使用图像作为背景可绘制对象,然后只做黑白分割图像。

我知道分割任何内容的唯一其他方法是使用 2 个视图,每个视图都有自己的背景颜色,并且每个视图都为 android:layout_weight 属性指定相同的正值(即 50/50)。然后他们将分割可用空间。

希望这有帮助!


2
投票

我发现做到这一点的方法是创建我自己的继承自RelativeLayout的类,我在其中重写了onMeasure方法。在此阶段,视图的高度已知,因此我获取了背景,并使用 setLayerInset 手动设置项目的插入。

    LayerDrawable bg = (LayerDrawable)getBackground();
    int h = getMeasuredHeight();
    bg.setLayerInset(1, 0, 0, 0, h/2);

这个方法虽然有点麻烦。在大多数情况下,克里斯提供的答案应该足够了。


2
投票

android:layout_weight=".20" 是以百分比实现的最佳方法

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
    android:id="@+id/logTextBox"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight=".20"
    android:maxLines="500"
    android:scrollbars="vertical"
    android:singleLine="false"
    android:text="@string/logText" >
</TextView>

</LinearLayout>

0
投票

如 swapnil 所说,最好的方法是在 LinearLayout 中使用layout_weight。 如果您要水平移动,请将layout_width设置为“fill_parent”,然后将layout_weight设置为您想要的百分比。从下面的内容开始,尝试使用layout_width值来感受它(或者添加更多视图以获得更好的理解)。

<LinearLayout    
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation = "horizontal" >

    <View
      android:layout_width="fill_parent"
      android:layout_height="30dip"
      android:layout_weight="0.5"
      android:background="#aaffaa" />
    <View
      android:layout_width="fill_parent"
      android:layout_height="30dip"
      android:layout_weight="0.5"
      android:background="#aaaaff" />
</LinearLayout>

0
投票

您可以使用 InsetDrawable 并将插图定义为分数。

这正是您要问的问题:

<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Black rectangle on the left half -->
    <item android:id="@+id/back_color_1">
        <inset android:insetRight="50%">
            <shape android:shape="rectangle">
                <solid android:color="#000000" />
            </shape>
        </inset>
    </item>

    <!-- White rectangle on the right half -->
    <item android:id="@+id/back_color_2">
        <inset android:insetLeft="50%">
            <shape android:shape="rectangle">
                <solid android:color="#FFFFFF" />
            </shape>
        </inset>
    </item>
</layer-list>

值得一提的是,图层列表中的可绘制对象是堆叠的,因此您可以使顶部颜色覆盖底部颜色。另外,如果您只定义一个颜色可绘制,您将得到一个矩形。

<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Full black background -->
    <item
        android:id="@+id/back_color_1"
        android:drawable="#000000">

    <!-- White color on the right half drawn on top -->
    <item android:id="@+id/back_color_2">
        <inset android:insetLeft="50%">
            <color android:color="#FFFFFF" />
        </inset>
    </item>
</layer-list>
© www.soinside.com 2019 - 2024. All rights reserved.