形状可绘制尺寸不起作用

时间:2012-05-03 15:27:12

标签: android drawable xml-layout shapedrawable

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

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

但是,当我将它指定给EditText的背景时,它只显示橙色背景而不是宽度为2dp的矩形。为什么不设置尺寸?我想创建一个透明的drawable,左侧是橙色矩形。我也把它包裹在选择器中:

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

8 个答案:

答案 0 :(得分:15)

对我来说,将项目的重力设置为“中心”即可解决此问题。 例如:

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

答案 1 :(得分:8)

只是指定user983447的答案 - size属性确实意味着一个比例。您应该为图层列表中的所有形状设置尺寸,并在缩放时将其用作比例 - 例如layout_weight的{​​{1}}属性。因此,最好将其命名为LinearLayout,而不是size

以下是如何在不使用weight属性的情况下实现顶部和底部白线的解决方法:

size

答案 2 :(得分:7)

它可以与foreground一起使用。看起来你无法设定背景的引力。但你可以在前台。我检查了API 21,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 :由于此答案有几张选票,您可能需要查看我实际使用的内容以显示新的指标点: https://gist.github.com/CapnSpellcheck/4d4638aefd085c703b9d990a21ddc1eb

答案 3 :(得分:4)

由于以下原因,我发现第一次使用Androider时,图层列表非常狡猾。乍一看,大多数人认为项目的顶部,底部,右侧,左侧属性是从顶部,底部,右侧,左侧。其中值如下:

<item android:top="10dp">

会从相应容器的顶部为您提供10dp的起点。不是这种情况。把它想象为顶部,底部,右侧,左侧的OFF。 <item android:top="10dp">仍然可以为你提供一个起点10dp OFF OF the top,但是当你想设置底部时会发生什么?

<item android:bottom="20dp">

这不会让你在距离TOP 20dp的底部,而是在容器的底部20dp OFF的底部。

所以,例如对于一个100dp的容器,如果你想要一个顶边从20dp开始,底边在40dp的矩形:

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

答案 4 :(得分:1)

我有类似的问题。

文档(http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape)说:

  

&LT;大小&GT;

     

形状的大小。   (...)

     

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

如果我理解正确,则表示“尺寸”标签不是用于设置尺寸,而是用于设置比例。

答案 5 :(得分:1)

shape的size属性将为drawable.getIntrinsicWidth&amp;提供值。 getIntrinsicHeight。

如果drawable的容器(例如ImageView,TextView)具有布局参数WRAP_CONTENT,那么如果drawable drawingState发生变化,容器尺寸将会改变。

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

ImageView仅通过state_selected上的可绘制维度更新/调整其维度,但不在state_activated上

答案 6 :(得分:0)

用过这个。

<?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中并设置视图背景。

答案 7 :(得分:0)

当您将形状用作 View 的背景时,它的大小将被忽略。当您通过 ImageView:

显示它时,它会起作用
<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"
    />