Android 形状可拉伸尺寸不起作用

Android 形状可拉伸尺寸不起作用,android,drawable,xml-layout,shapedrawable,Android,Drawable,Xml Layout,Shapedrawable,我有一个非常简单的形状,我想设置宽度: <shape android:shape="rectangle"> <solid android:color="@color/orange"/> <size android:width="2dp"/> </shape> 但是,当我将其指定给EditText的背景时,它只显示橙色背景,而不是宽度为2dp的矩形。为什么设置大小不起作用?我想创建一个透明的可绘制图形,左侧有一个橙色矩形。我还将

我有一个非常简单的形状,我想设置宽度:

<shape android:shape="rectangle">
    <solid android:color="@color/orange"/>
    <size android:width="2dp"/>
</shape>

但是,当我将其指定给EditText的背景时,它只显示橙色背景,而不是宽度为2dp的矩形。为什么设置大小不起作用?我想创建一个透明的可绘制图形,左侧有一个橙色矩形。我还将其包装在选择器中:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true">
        <shape android:shape="rectangle">
            <solid android:color="@color/orange"/>
            <size android:width="2dp" android:height="6dp"/>
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@android:color/transparent"/>
        </shape>
    </item>
</selector>

我试着增加高度,只是想看看它是否会改变尺寸。没有。就像完全忽略了尺寸一样。WTF?

我也有类似的问题

文件()说:

形状的大小。 (……)

注意:默认情况下,形状缩放到与此处定义的尺寸成比例的容器视图大小。在ImageView中使用形状时,可以通过将android:scaleType设置为“中心”来限制缩放


如果我理解正确,这意味着“size”标记不是用于设置大小,而是用于设置比例。

只是为了指定用户983447的答案-size属性实际上是指比例。您应该为图层列表中的所有形状设置大小,并在缩放时作为比例使用-如
LinearLayout
layout\u weight
属性。因此,最好不要将其命名为
size
,而应命名为
weight

以下是如何在不使用
size
属性的情况下实现顶部和底部白线的工作:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <solid android:color="#fff" />
        </shape>
    </item>
    <item android:top="1dp" android:bottom="1dp">
        <shape>
            <solid android:color="#888" />
        </shape>
    </item>
</layer-list>

使用了此选项

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

<stroke
    android:color="@color/lgray"
    android:width="1dip" />

<corners
    android:bottomLeftRadius="0dip"
    android:bottomRightRadius="0.1dip"
    android:topLeftRadius="0dip"
    android:topRightRadius="0.1dip" />

<solid android:color="@color/White" />

</shape>


将这个rectangle.xml放在drawable.xml中,并设置您的视图背景。

我第一次发现图层列表非常曲折,因为以下原因。乍一看,大多数人会认为项目top、bottom、right、left属性来自top、bottom、right、left。其中,以下值:

<item android:top="10dp">

从相应容器顶部算起,您将获得一个10dp的起点。事实并非如此。把它想象成从上、下、右、左<代码>仍会使您的起点偏离顶部10dp,但当您想要设置底部时会发生什么

<item android:bottom="20dp">

这不会使您从顶部看到20 DP的底部,而是从容器底部看到20 DP的底部

例如,对于一个100dp的容器,如果您想要一个上边缘从20dp开始,下边缘从40dp开始的矩形:

<item android:top="20" android:bottom="60dp">

形状的size属性将提供drawable.getIntrinsicWidth和getIntrinsicHeight的值

如果可绘图的容器(例如ImageView、TextView)具有布局参数WRAP_CONTENT,则如果可绘图的drawingState更改,则容器尺寸将更改

但是在android框架的ImageView drawingState实现中存在一个bug


ImageView仅在选定状态下按可绘制尺寸更新/调整其尺寸,但在激活状态下不更新/调整其尺寸

它可以使用
前景
。看起来你无法设置背景的重力。但你可以站在前景上。我检查了API21、23和24(以及Studio设计预览),下面在ImageView上放置了一个实心圆点

<shape android:shape="oval" xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/colorPrimary" />
    <size android:height="8dp" android:width="8dp" />
</shape>

使用布局代码段

    <ImageView
            android:foreground="@drawable/list_new_dot"
            android:foregroundGravity="right|center_vertical"
            tools:src="@drawable/model_1_sm"
            />

更新:虽然它在布局设计工具中似乎起作用,但在模拟器中看起来不一样更新2:由于此答案有几票,您可能需要检查我实际使用的内容,以便显示新的指示点:
对我来说,将物品的重心设置为“中心”解决了问题。 例如:

<item android:id="@android:id/progress" android:gravity="center">
    <clip>
        <shape>
            <size android:height="2dp"/>
            <solid android:color="@color/my_color"/>
        </shape>
    </clip>
</item>

将形状用作
视图的背景时,形状的大小将被忽略。当您通过
图像视图显示它时,它将工作:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#FF7700" />
    <size android:width="20dp" android:height="20dp"/>
</shape>

在布局XML中:

<!-- will have the size of 20dp x 20dp -->
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/my_shape"
    />


所以它说宽度/高度是整数,但您发布的内容听起来可能是百分比(0..1),或者与容器的维度(0..view.width)一样大?!或者你是说它有道理,你知道它是如何工作的,并且比文档解释得更好?我不完全明白你的意思。无论哪种方式,它都可能保证一个bug可以清除极其糟糕的文档,或者修复代码,使其实际上是宽度和高度。它如何不适合您?它就像一个符咒。也许问题出在布局的设置上?看,如果你的布局在高度和宽度上都有包裹内容,那就很简单了。被忽略,并将始终创建一个填充视图的形状。不总是,当然不使用drawableTop/Bottom/etc属性。@Daler您所说的只是忽略大小并填充视图。你应该测试一下。这与布局设置无关。当我发布问题时,我实际上理解了这一点。对于所有值(即顶部、左侧、底部、右侧),将其全部设置为顶部、左侧相对没有什么意义,因为在不知道容器大小的情况下,您永远无法定义扩展以匹配其父容器的形状。在这个特殊的例子中,我有一个需要固定大小的形状,因此我想使用宽度/高度来设置尺寸,但文档没有解释宽度/高度的坐标系,所以你只能猜测。谢谢,这是一个很好的解释!让我明白我错在哪里,并在几个小时后最终解决问题!但我为你竖起大拇指+1由于gpu透支(当你只需要两行像素时,两个形状填充所有像素)这会导致性能不佳@Gilian如果我错了,请纠正我,但此xml会通过aapt编译成可绘制的某个子类,因此编译完成后它只是一个java类,性能将与