是否可以在XML中创建一个固定宽度的绘图器?

时间:2022-11-10 15:02:08

I'm trying to simplify some graphics from requiring a 9-patch for each density, to just using an XML drawable. It's a fairly simple graphic:

我试图简化一些图形,从每个密度都需要9个补丁,到使用XML可绘图。这是一个相当简单的图表:

是否可以在XML中创建一个固定宽度的绘图器?

2 Segments:

2段:

  • 8dp wide for both segments
  • 两段宽度为8dp
  • Top segment is 2dp tall
  • 顶部部分是2dp高
  • Bottom segment fills the view
  • 底部段填充视图
  • Right side is filled with transparency
  • 右边充满了透明度。

Giving this result when set as a background image:

将此结果设置为背景图像时:

是否可以在XML中创建一个固定宽度的绘图器?

It seems like this should be fairly simple to recreate as an XML drawable, and would avoid creating 5 different 9-patch graphics. However, I've looked into layer-list drawables, inset drawables, clip drawables -- they all seem to require that you know the size of the view beforehand (e.g. to keep it 2dp for an inset, you'd need to set insetRight to the width of the view - 2dp). I also tried using the shape tag's size tag, but that doesn't keep a fixed size, just a fixed proportion (so for taller views it will scale proportionally).

看起来这应该是相当简单的重新创建作为一个XML可绘图,并将避免创建5个不同的9补丁图形。然而,我已经研究了层列表的可绘制性、inset可绘制性、剪辑可绘制性——它们似乎都要求您事先知道视图的大小(例如,为了使inset保持为2dp,您需要将insetRight设置为视图的宽度2dp)。我也尝试过使用形状标签的大小标签,但是它没有固定的大小,只是固定的比例(所以对于更高的视图,它会按比例缩放)。

Am I overlooking something simple, or is this something I'd have to resort to checking programatically after layout?

我是否忽略了一些简单的东西,或者这是我需要在布局之后通过程序检查的东西?

3 个解决方案

#1


4  

This is not possible. This is the best solution I found. Only xml, no coding, no bitmaps, no additional views )

这是不可能的。这是我找到的最好的解决办法。只有xml,没有编码,没有位图,没有其他视图)

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

        <solid
            android:color="#ff0000" />
    </shape>
</item>
<item
    android:top="4dp">
    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">

        <solid
            android:color="#00ff00" />
    </shape>
</item>

<item
    android:left="10dp">
    <shape
        android:shape="rectangle">
        <solid
            android:color="#FFFFFF" />
    </shape>
</item> 
</layer-list>

是否可以在XML中创建一个固定宽度的绘图器?

Via code you can make your own drawble type with desired behavior. Xml drawable very limited. IMHO my suggestion is the closest to what you demanded.

通过代码,您可以使用所需的行为创建自己的drawble类型。Xml可拉的非常有限。我的建议和你要求的最接近。

#2


2  

I think the answer by @Leonidos is very close to what I'm proposing, except I'm using the drawable as a separate view instead of the background for the entire list detail layout. This allows it to be a fixed width and to let the item text have its own background. Let me know if I'm not understanding your constraints.

我认为@Leonidos的答案非常接近我的提议,除了我使用drawable作为一个独立的视图,而不是整个列表细节布局的背景。这允许它是一个固定的宽度,并让项目文本有自己的背景。如果我不理解你的限制,请告诉我。

The following file is "res/drawable/list_left_border"

下面的文件是“res/drawable/list_left_border”

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape android:shape="rectangle" >
            <solid android:color="#e76e63" />
        </shape>
    </item>
    <item android:top="2dp">
        <shape android:shape="rectangle" >
            <solid android:color="#ffc257" />
        </shape>
    </item>

</layer-list>

Then on the layout for the list item in "res/layout/list_item.xml"

然后在“res/layout/list_item.xml”中的列表项的布局上

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity" >

    <View
        android:background="@drawable/list_left_border"
        android:layout_width="8dp"
        android:layout_height="match_parent" />

    <TextView
        android:id="@+id/listItemText"
        android:textSize="15sp"
        android:padding="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

Here's the image. The background is pink to show that the list item is transparent.

这是形象。背景是粉色,以显示列表项是透明的。

是否可以在XML中创建一个固定宽度的绘图器?

#3


2  

I'm going to go one further and suggest a slight reworking of AndroidGuy's answer, which itself includes Leonidos' answer.

我将更进一步,建议对AndroidGuy的回答进行一些修改,其中也包含了Leonidos的回答。

Same drawable XML, different list item XML as follows:

相同的可绘制XML,不同的列表项XML如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="100sp">

    <ImageView
        android:layout_width="8dp"
        android:layout_height="match_parent"
        android:src="@drawable/list_left_border" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="Item Text"
        android:textSize="22sp" >
    </TextView>
</FrameLayout>

The FrameLayout gives the original background behaviour, rather than separation into a new view.

框架布局给出了原始的背景行为,而不是将其分离到新的视图中。

I've shown this below, with backgrounds to demonstrate:

我在下面展示了这一点,并用背景来演示:

是否可以在XML中创建一个固定宽度的绘图器?

This doesn't really deserve the points though.

但这并不是真正值得的。

#1


4  

This is not possible. This is the best solution I found. Only xml, no coding, no bitmaps, no additional views )

这是不可能的。这是我找到的最好的解决办法。只有xml,没有编码,没有位图,没有其他视图)

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

        <solid
            android:color="#ff0000" />
    </shape>
</item>
<item
    android:top="4dp">
    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">

        <solid
            android:color="#00ff00" />
    </shape>
</item>

<item
    android:left="10dp">
    <shape
        android:shape="rectangle">
        <solid
            android:color="#FFFFFF" />
    </shape>
</item> 
</layer-list>

是否可以在XML中创建一个固定宽度的绘图器?

Via code you can make your own drawble type with desired behavior. Xml drawable very limited. IMHO my suggestion is the closest to what you demanded.

通过代码,您可以使用所需的行为创建自己的drawble类型。Xml可拉的非常有限。我的建议和你要求的最接近。

#2


2  

I think the answer by @Leonidos is very close to what I'm proposing, except I'm using the drawable as a separate view instead of the background for the entire list detail layout. This allows it to be a fixed width and to let the item text have its own background. Let me know if I'm not understanding your constraints.

我认为@Leonidos的答案非常接近我的提议,除了我使用drawable作为一个独立的视图,而不是整个列表细节布局的背景。这允许它是一个固定的宽度,并让项目文本有自己的背景。如果我不理解你的限制,请告诉我。

The following file is "res/drawable/list_left_border"

下面的文件是“res/drawable/list_left_border”

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape android:shape="rectangle" >
            <solid android:color="#e76e63" />
        </shape>
    </item>
    <item android:top="2dp">
        <shape android:shape="rectangle" >
            <solid android:color="#ffc257" />
        </shape>
    </item>

</layer-list>

Then on the layout for the list item in "res/layout/list_item.xml"

然后在“res/layout/list_item.xml”中的列表项的布局上

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity" >

    <View
        android:background="@drawable/list_left_border"
        android:layout_width="8dp"
        android:layout_height="match_parent" />

    <TextView
        android:id="@+id/listItemText"
        android:textSize="15sp"
        android:padding="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

Here's the image. The background is pink to show that the list item is transparent.

这是形象。背景是粉色,以显示列表项是透明的。

是否可以在XML中创建一个固定宽度的绘图器?

#3


2  

I'm going to go one further and suggest a slight reworking of AndroidGuy's answer, which itself includes Leonidos' answer.

我将更进一步,建议对AndroidGuy的回答进行一些修改,其中也包含了Leonidos的回答。

Same drawable XML, different list item XML as follows:

相同的可绘制XML,不同的列表项XML如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="100sp">

    <ImageView
        android:layout_width="8dp"
        android:layout_height="match_parent"
        android:src="@drawable/list_left_border" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="Item Text"
        android:textSize="22sp" >
    </TextView>
</FrameLayout>

The FrameLayout gives the original background behaviour, rather than separation into a new view.

框架布局给出了原始的背景行为,而不是将其分离到新的视图中。

I've shown this below, with backgrounds to demonstrate:

我在下面展示了这一点,并用背景来演示:

是否可以在XML中创建一个固定宽度的绘图器?

This doesn't really deserve the points though.

但这并不是真正值得的。