Android 是否可以在XML中创建固定宽度的绘图?
我正在尝试简化一些图形,从每个密度需要9个补丁,到只使用XML可绘制。这是一个相当简单的图形: 2部分:Android 是否可以在XML中创建固定宽度的绘图?,android,drawable,xml-drawable,layer-list,Android,Drawable,Xml Drawable,Layer List,我正在尝试简化一些图形,从每个密度需要9个补丁,到只使用XML可绘制。这是一个相当简单的图形: 2部分: 两段均为8dp宽 上段为2 dp高 底部线段填充视图 右边是透明的 设置为背景图像时给出此结果: 这看起来应该很容易重新创建为XML绘图,并且可以避免创建5个不同的9-patch图形。然而,我已经研究了图层列表可绘制、插入可绘制、剪辑可绘制——它们似乎都要求您事先知道视图的大小(例如,为了使插入保持2dp,您需要将insetRight设置为视图的宽度-2dp)。我还尝试使用shape
- 两段均为8dp宽
- 上段为2 dp高
- 底部线段填充视图
- 右边是透明的
insetRight
设置为视图的宽度-2dp)。我还尝试使用shape
标记的size
标记,但这并没有保持固定的大小,只是保持固定的比例(因此对于较高的视图,它将按比例缩放)
我是否忽略了一些简单的东西,或者这是我在布局后必须通过程序检查的东西 这是不可能的。这是我找到的最好的解决办法。仅xml,无编码,无位图,无其他视图)
通过代码,您可以使用所需的行为创建自己的drawble类型。Xml可绘制性非常有限。我认为我的建议与您的要求最为接近。我认为@Leonidos的回答与我的建议非常接近,只是我使用了可绘制视图,而不是整个列表细节布局的背景。这允许它是一个固定的宽度,并让项目文本有自己的背景。如果我不理解您的限制,请告诉我 以下文件是“res/drawable/list\u left\u border”
然后是“res/layout/list_item.xml”中列表项的布局
这是图片。背景为粉红色,表示列表项是透明的
我将进一步建议对AndroidGuy的答案稍加修改,其中包括Leonidos的答案 相同的可绘制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>
FrameLayout提供原始背景行为,而不是分离到新视图中
我在下面展示了这一点,并用背景来演示:
不过,这并不值得打分。如果最后一个项目的背景是纯色,则此选项有效。如果它应该是透明的(正如问题中所要求的那样),那么它将不起作用。附加视图是欺骗)@Leonidos是一个约束条件,即可绘制视图应该是整个细节项布局的背景?在我的示例中,这将是LinearLayout视图。或者它需要成为TextView的背景吗?我的意思是作者不需要额外的视图)@Leonidos同意,最好不要有额外的视图——不过,这可能仍然是一个很好的解决方案。我认为您的两种解决方案似乎都非常聪明和可行,但不可否认的是,drawable最好只是列表项的背景(LinearLayout或其他任何形式),因为视图实际上会被调整大小。谢谢你们两位的回答——我将再等一段时间接受其中一个,看看是否有其他解决方案被提出。:)对此的改进是在
TextView
上使用android:drawablelft=“@drawable/list\u left\u border”
。然后,您可以删除额外的视图
。您是否尝试在每个项目上为TextView绘制背景?抱歉,忽略这个评论@kcopock,我看到你已经对我的答案做出了回应。
<?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>
<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>
<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>