Android 来自 xml 选择器的填充图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12247723/
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 10:10:56  来源:igfitidea点击:

padding image from xml selector

android

提问by user1592470

I'm trying to padd my button background image of my main menu (I'm using a selector for the different states), doing it on this way (buttoninicio_custom.xml):

我正在尝试填充我的主菜单的按钮背景图像(我正在使用不同状态的选择器),以这种方式(buttoninicio_custom.xml):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/botoninicial_pressed"
          android:state_pressed="true">

            <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />

    </item>
    <item android:drawable="@drawable/botoninicial_pressed"
          android:state_focused="true">

            <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />

    </item>
    <item android:drawable="@drawable/botoninicial">
            <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </item>
</selector>

..but the padding has no effect. What I should do to solve this problem??

..但填充没有效果。我应该怎么做才能解决这个问题??

I already used "bitmap" tag inside each "item" tag with the padding inside, but it's still doing anything!!!

我已经在每个带有填充的“item”标签中使用了“bitmap”标签,但它仍然在做任何事情!!!

My main button looks this way:

我的主按钮看起来像这样:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button2"
        android:layout_width="180dp"
        android:layout_height="50dp"
        android:layout_marginBottom="15dp"
        android:textSize="16sp"
        android:textColor="#FFFFFF"
        android:background="@drawable/buttoninicio_custom"
        android:text="@string/idmMENU2" />
</LinearLayout>

"Because if I set android:padding inside the "button" tag, it pads me the text not the background image... The problem it's that when I pressed the main button: My image background change correctly but the new image appear cut."

“因为如果我在“按钮”标签内设置 android:padding,它会填充文本而不是背景图像......问题是当我按下主按钮时:我的图像背景正确更改,但新图像出现剪切。 ”

回答by Oliv

Wrap it in a layer-list:

将它包裹在一个layer-list

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:bottom="3dp"
        android:left="3dp"
        android:right="3dp"
        android:top="1dp">

        <selector xmlns:android="http://schemas.android.com/apk/res/android">
            <!-- your original selector content -->
        </selector>
    </item>
</layer-list>

回答by karn

paddingattribute is of no use under the tag item. If you press ctrl+space(autocomplete) then android won't suggest using this tag though android won't give any error. You can define padding when you are creating a shape drawable.Something like this:

padding属性在标签item下没有用。如果您按 ctrl+space(autocomplete) 则 android 不会建议使用此标签,尽管 android 不会给出任何错误。您可以在创建可绘制形状时定义填充。像这样:

<item android:state_pressed="true">
    <shape >
        <solid
            android:color="#2c68e7" />
        <stroke 
            android:width="1dp"
            android:color="#2c68e7" />
        <corners
            android:radius="4dp" />
        <padding
            android:left="10dp"
            android:right="10dp"
            android:top="10dp"
            android:bottom="10dp" />
    </shape>
</item> 

But you cannot use an image resource in the shape drawable.

但是您不能在可绘制形状中使用图像资源。

The better solution would be to use images of same size and dimension for the view.

更好的解决方案是为视图使用相同大小和尺寸的图像。