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

发布于 2024-11-08 04:25:34 字数 832 浏览 0 评论 0原文

我正在尝试创建一个简单的 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>

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

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

PS:您可以在 CSS 或 XAML 中执行此操作。

I'm trying to create a simple Drawable that I want to set as the background for a view (using setBackgroundDrawable). I simply want to divide the background of the drawable into 2 equal rectangles (50% - 50%), the first want filled with black, the second with white:

<?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>

How can I specify that each shape should have a width of 50% in the drawable XML definition file? Something like android:width="50%".

(I'm working on Android 3.0, but I think it is a general Android question.)

P.S: You can do this in CSS or XAML.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

过期以后 2024-11-15 04:25:34

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

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(...) 中自行执行计算。

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

You can't specify a percentage. You need to specify a Dimension value to it.

android:width is defined here: http://developer.android.com/reference/android/R.attr.html#width :

(emphasis mine)

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).

For a definition of each dimension type, see: http://developer.android.com/guide/topics/resources/more-resources.html#Dimension

You will need to create your own attribute (see styleable) and perform the calculations yourself in onDraw(...).

See these two questions and the links therein for examples:

两人的回忆 2024-11-15 04:25:34

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

实现此目的的最简单方法似乎是使用图像作为背景可绘制对象,然后仅执行黑白分割图像。

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

希望这有帮助!

there is no true percentage setting, but you might get close.

it seems like the easiest way to achieve this is to use an image as your background drawable and just do a black and white split image.

the only other way I know to split anything is to use 2 views, each with its own bg color, and each given the same positive value for android:layout_weight attribute (ie. 50/50). they will then split the available space.

hope this helps!

祁梦 2024-11-15 04:25:34

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

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

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

The way I found to do this was by creating my own class inheriting from RelativeLayout, where I override the onMeasure method. At this stage, the height of the view is known, so I fetched the background, and manually set the inset of the items using setLayerInset.

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

This method is a bit cumbersome though. In most cases, the answer provided by chris should be sufficient.

蹲墙角沉默 2024-11-15 04:25:34

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>

android:layout_weight=".20" is best way to implement in percentage

<?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>
如日中天 2024-11-15 04:25:34

您可以使用 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>

You can use an InsetDrawable with insets defined as fractions.

This is the exact thing you're asking:

<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>

It's worth mentioning that drawables in a layer-list are stacked, so you can make the top color cover the bottom one. Also, you'll get a rectangle if you just define a color drawable.

<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>
甜味超标? 2024-11-15 04:25:34

正如 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>

The best way as swapnil stated, is to use layout_weight in a LinearLayout.
If you're going horizontally, set layout_width to "fill_parent" then set the layout_weight to a percentage that you want. Start with something like below and play around with the layout_width values to get a feel of it (or add more Views just to get a better understanding).

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文