I'm having problem with setting size of drawable in layer-list. My XML looks like this:
我在layer-list中设置drawable的大小有问题。我的XML看起来像这样:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/menu_drawable1"/>
<item android:drawable="@drawable/menu_drawable2"/>
</layer-list>
Drawable1 is bigger than Drawable2, so Drawable2 gets resized. How to override this and set the original size of Drawable2? I don't want it to resize. I tried with setting width and height but that doesn't work. Also the drawables aren't actually bitmaps, they are selectos, so I can't use the bitmap trick.
Drawable1比Drawable2大,因此Drawable2会调整大小。如何覆盖它并设置Drawable2的原始大小?我不希望它调整大小。我尝试设置宽度和高度,但这不起作用。 drawables实际上并不是位图,它们是selectos,所以我不能使用位图技巧。
It looks like I've got half of the solution by using "left" and "right", but still, this doesn't let me set the exact size of the image automatically. Now my xml looks like this:
通过使用“left”和“right”看起来我已经有了一半的解决方案,但是,这仍然不能让我自动设置图像的确切大小。现在我的xml看起来像这样:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/menu_drawable1"/>
<item android:drawable="@drawable/menu_drawable2" android:right="150dp"/>
</layer-list>
4 个解决方案
#1
5
You can use android:width and android:height parameters with desired values.
你可以使用android:width和android:height参数和所需的值。
Below is the sample drawable layer-list and screen shot. I used the vector drawable from google material icons and own selector drawable.
下面是示例可绘制图层列表和屏幕截图。我使用了google材料图标中的矢量drawable和自己的选择器drawable。
Original circle.xml drawable shape have 100dp x 100dp size, but into drawable.xml I override this values to 24dp x 24dp by android:width and android:height parameters.
原始的circle.xml可绘制形状有100dp x 100dp大小,但是在drawable.xml中我通过android:width和android:height参数将这个值覆盖为24dp x 24dp。
I hope this help!
我希望这有帮助!
drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/circle" android:height="24dp" android:width="24dp"/>
<item android:drawable="@drawable/ic_accessibility_black_24dp"/>
<item android:drawable="@drawable/ic_http_white_24dp" android:gravity="center" android:top="-3dp" android:height="5dp" android:width="5dp"/>
</layer-list>
circle.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:innerRadius="20dp" android:shape="oval">
<solid android:color="#CCC" />
<size android:width="100dp" android:height="100dp"/>
</shape>
</item>
</selector>
#2
3
You can use inset tag for this. For example
您可以为此使用插入标记。例如
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/menu_drawable1"/>
<item>
<inset android:drawable="@drawable/menu_drawable2"
android:insetRight="50dp"
android:insetTop="50dp"
android:insetLeft="50dp"
android:insetBottom="50dp" />
</item>
This will create paddings around you menu_drawable2 so it won`t be scaled. Just set the dimensions that you require.
这将在menu_drawable2周围创建填充,因此不会缩放。只需设置所需的尺寸即可。
#3
0
Hope this will help :-
You can use a nine patch drawable for menu_drawable2
. You can make menu_drawable2
like having transparent region around it edges. Use this transparent region to specify stretchable region in nine patch.
希望这会有所帮助: - 您可以为menu_drawable2使用九个可绘制的补丁。你可以使menu_drawable2像边缘一样有透明区域。使用此透明区域指定九个补丁中的可伸展区域。
#4
-3
i dont know its works or not but did you tried to seperate them from each other?
我不知道它的作品与否,但你是否试图将它们彼此分开?
something like this:
像这样的东西:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/menu_drawable1"/>
</layer-list>
<layer-list>
<item android:drawable="@drawable/menu_drawable2"/>
</layer-list>
or put something else betwen them like empty layout with height and width 0dp.
或者把它们放在其他东西之间,就像高度和宽度为0dp的空布局一样。
#1
5
You can use android:width and android:height parameters with desired values.
你可以使用android:width和android:height参数和所需的值。
Below is the sample drawable layer-list and screen shot. I used the vector drawable from google material icons and own selector drawable.
下面是示例可绘制图层列表和屏幕截图。我使用了google材料图标中的矢量drawable和自己的选择器drawable。
Original circle.xml drawable shape have 100dp x 100dp size, but into drawable.xml I override this values to 24dp x 24dp by android:width and android:height parameters.
原始的circle.xml可绘制形状有100dp x 100dp大小,但是在drawable.xml中我通过android:width和android:height参数将这个值覆盖为24dp x 24dp。
I hope this help!
我希望这有帮助!
drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/circle" android:height="24dp" android:width="24dp"/>
<item android:drawable="@drawable/ic_accessibility_black_24dp"/>
<item android:drawable="@drawable/ic_http_white_24dp" android:gravity="center" android:top="-3dp" android:height="5dp" android:width="5dp"/>
</layer-list>
circle.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:innerRadius="20dp" android:shape="oval">
<solid android:color="#CCC" />
<size android:width="100dp" android:height="100dp"/>
</shape>
</item>
</selector>
#2
3
You can use inset tag for this. For example
您可以为此使用插入标记。例如
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/menu_drawable1"/>
<item>
<inset android:drawable="@drawable/menu_drawable2"
android:insetRight="50dp"
android:insetTop="50dp"
android:insetLeft="50dp"
android:insetBottom="50dp" />
</item>
This will create paddings around you menu_drawable2 so it won`t be scaled. Just set the dimensions that you require.
这将在menu_drawable2周围创建填充,因此不会缩放。只需设置所需的尺寸即可。
#3
0
Hope this will help :-
You can use a nine patch drawable for menu_drawable2
. You can make menu_drawable2
like having transparent region around it edges. Use this transparent region to specify stretchable region in nine patch.
希望这会有所帮助: - 您可以为menu_drawable2使用九个可绘制的补丁。你可以使menu_drawable2像边缘一样有透明区域。使用此透明区域指定九个补丁中的可伸展区域。
#4
-3
i dont know its works or not but did you tried to seperate them from each other?
我不知道它的作品与否,但你是否试图将它们彼此分开?
something like this:
像这样的东西:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/menu_drawable1"/>
</layer-list>
<layer-list>
<item android:drawable="@drawable/menu_drawable2"/>
</layer-list>
or put something else betwen them like empty layout with height and width 0dp.
或者把它们放在其他东西之间,就像高度和宽度为0dp的空布局一样。