Android 形状可绘制大小不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10434517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:44:29  来源:igfitidea点击:

Shape Drawable Size not working

androiddrawablexml-layoutshapedrawable

提问by chubbsondubs

I have a very simple shape that I want to set the width of:

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

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

However, when I assign this to the background of a EditText it just shows an orange background instead of a rectangle of width 2dp. Why isn't setting the size working? I want to create a transparent drawable with a orange rectangle on the left side. I also have this wrapped in a selector:

但是,当我将它分配给 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>

I've tried adding height just to see if it would change the size. It doesn't. It's like its completely ignoring the size. WTF?

我试过增加高度只是为了看看它是否会改变大小。它没有。这就像它完全忽略了大小。跆拳道?

回答by Simple UX Apps

For me, setting the gravity of the item to "center" solved the issue. For example:

对我来说,将物品的重力设置为“中心”解决了这个问题。例如:

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

回答by androidguy

It can work with a foreground. It seems like you can't set a background's gravity. But you can on a foreground. I checked API 21, 23 and 24 (well, with the Studio design preview) and the following places a solid circle dot on the ImageView.

它可以与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>

With the layout snippet

使用布局片段

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

UPDATE: While it appears to work in the layout design tool, it doesn't look the same in the emulator. UPDATE 2: Since this answer has a few votes, you might want to check what I actually used in order to show a new indicator dot: https://gist.github.com/CapnSpellcheck/4d4638aefd085c703b9d990a21ddc1eb

更新:虽然它似乎在布局设计工具中工作,但在模拟器中看起来不一样。更新 2:由于此答案有几票,您可能想检查我实际使用的内容以显示新的指标点:https: //gist.github.com/CapnSpellcheck/4d4638aefd085c703b9d990a21ddc1eb

回答by AlexD

Just to specify the user983447's answer - the size attribute does really mean a proportion. You should set the size for all shapes in your layer-list and it'll be used a as a proportion when scaling - like the layout_weightattribute of LinearLayout. So it's better to name it not a sizebut a weight

只是为了指定 user983447 的答案 - size 属性确实意味着一个比例。您应该设置大小为您的层列表中的所有形状和缩放时,它会被用来作为一个比例-喜欢layout_weight的属性LinearLayout。所以最好把它命名为 not a sizebut aweight

Below is a work-around how to implement top and bottom white lines without using the sizeattribute:

下面是如何在不使用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>

回答by user3442825

I found the layer-list to be very devious for a first time Androider because of the following. At first glance most would think item top,bottom,right,left attributes are FROM the top,bottom,right,left. Where a value of the following:

由于以下原因,我第一次发现层列表非常狡猾。乍一看,大多数人会认为 item top,bottom,right,left 属性来自于 top,bottom,right,left。具有以下值的地方:

<item android:top="10dp">

Would net you a starting point 10dp from the top of the respective container. This is not the case. Think of it as OFF OF the top,bottom,right,left. <item android:top="10dp">will still net you a starting point 10dp OFF OF the top, but what happens when you want to set the bottom?

会为您提供距相应容器顶部 10dp 的起点。不是这种情况。将其视为顶部、底部、右侧、左侧的 OFF。<item android:top="10dp">仍然会给你一个离顶部 10dp 的起点,但是当你想设置底部时会发生什么?

<item android:bottom="20dp">

This will not get you a bottom at 20dp from the TOP, rather a bottom of 20dp OFF OF the BOTTOM of the container.

这不会让你在距离顶部 20dp 的底部,而是在容器底部关闭 20dp 的底部。

So, for example with a 100dp container, if you wanted a rectangle with a top edge starting at 20dp and a bottom edge at 40dp:

因此,例如对于 100dp 的容器,如果您想要一个矩形,其顶部边缘从 20dp 开始,底部边缘从 40dp 开始:

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

回答by user882729

shape's size attribute will provide the value for drawable.getIntrinsicWidth & getIntrinsicHeight.

shape 的 size 属性将为 drawable.getIntrinsicWidth 和 getIntrinsicHeight 提供值。

if the drawable's container(e.g. ImageView, TextView) has the layout param WRAP_CONTENT, then the container dimension will change if the drawable drawingState change.

如果 drawable 的容器(例如 ImageView、TextView)具有布局参数 WRAP_CONTENT,那么如果 drawabledrawingState 更改,则容器尺寸将更改。

but there are a bug in android framework in ImageView drawingState implementation

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

ImageView only update/resize its dimension by the drawable dimension on state_selected but don't on state_activated

ImageView 仅通过 state_selected 上的可绘制尺寸更新/调整其尺寸,而不是 state_activated

回答by user983447

I had similar problem.

我有类似的问题。

Documentation ( http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape) says:

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

<size>

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".

<尺寸>

形状的大小。(……)

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

If I understand correctly, it means that "size" tag is not for setting size but for setting proportions.

如果我理解正确,这意味着“尺寸”标签不是用于设置尺寸而是用于设置比例。

回答by Harshid

used this.

用了这个。

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

put this rectangle.xmlto drawable.and set your view background.

把这个rectangle.xml到 drawable.and 设置你的视图背景。