Shape Drawable Size not working
我有一个非常简单的形状,想要设置宽度:
1 2 3 4 | <shape android:shape="rectangle"> <solid android:color="@color/orange"/> <size android:width="2dp"/> </shape> |
但是,当我将其分配给EditText的背景时,它仅显示橙色背景,而不是宽度为2dp的矩形。 为什么设置尺寸无效? 我想创建一个透明的可绘制对象,其左侧为橙色矩形。 我也将其包装在选择器中:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <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?
对我来说,将项目的重力设置为"中心"可以解决此问题。
例如:
1 2 3 4 5 6 7 8 | <item android:id="@android:id/progress" android:gravity="center"> <clip> <shape> <size android:height="2dp"/> <solid android:color="@color/my_color"/> </shape> </clip> </item> |
仅用于指定user983447的答案-size属性确实表示比例。您应该在图层列表中设置所有形状的尺寸,并且在缩放时将其用作比例-如
下面是一种变通方法,如何在不使用
1 2 3 4 5 6 7 8 9 10 11 12 | <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> |
它可以与
1 2 3 4 | <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> |
带有布局片段
1 2 3 4 5 | <ImageView android:foreground="@drawable/list_new_dot" android:foregroundGravity="right|center_vertical" tools:src="@drawable/model_1_sm" /> |
更新:虽然它在布局设计工具中似乎可以工作,但在模拟器中看起来却不一样。更新2:由于此答案有几票,所以您可能需要检查一下我实际使用的内容以显示新的指标点:
https://gist.github.com/CapnSpellcheck/4d4638aefd085c703b9d990a21ddc1eb
我发现,由于以下原因,第一次使用Androider时,层列表非常不完整。乍一看,大多数人会认为项目top,bottom,right,left属性是FROM top,bottom,right,left。其中值以下:
1 | <item android:top="10dp"> |
从各个容器的顶部开始,您将获得10dp的起点。不是这种情况。认为它不在顶部,底部,右侧,左侧。
1 | <item android:bottom="20dp"> |
这不会使您从顶部下降20dp的底部,而是使容器底部的底部下降20dp的底部。
因此,例如对于一个100dp的容器,如果您想要一个矩形的顶部边缘从20dp开始,底部边缘从40dp开始:
1 | <item android:top="20" android:bottom="60dp"> |
shape的size属性将提供drawable.getIntrinsicWidth和getIntrinsicHeight的值。
如果可绘制对象的容器(例如ImageView,TextView)具有布局参数WRAP_CONTENT,则如果可绘制对象drawingState更改,则容器尺寸也会更改。
但是在ImageView drawingState实现中的android框架中存在错误
ImageView仅在state_selected上通过drawable尺寸更新/调整其尺寸,而不在state_activated上更新/调整其尺寸
我有类似的问题。
文档(http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape)指出:
The size of the shape.
(...)Note: The shape scales to the size of the container View proportionate to the dimensions defined here, by default. When you use the shape in an ImageView, you can restrict scaling by setting the android:scaleType to"center".
如果我理解正确,则意味着" size"标签不是用于设置尺寸,而是用于设置比例。
用这个。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?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> |
将此矩形.xml放入drawable。并设置视图背景。