设置背景时按钮会变大-如何使其变小 [英] Button gets bigger when background is set - How to make it small

查看:65
本文介绍了设置背景时按钮会变大-如何使其变小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在设置背景之前使按钮变窄或恢复正常.

I want to make my button narrowed or back to normal before it was set with background.

我知道使用背景色可以在具有相同背景色的情况下使用,但是我的问题是我在背景上使用了选择器.将选择器设置为按钮背景时,它变宽了.当我将背景切换为backgroundtint时,颜色变得不同(例如对我来说是紫罗兰色)并且在按下时它不会改变颜色.通过设置负值或正值,填充无法正常工作.我还可以做些什么 ?我的目标是我的按钮在单击时会改变颜色,但是它的高度很窄/默认大小,如在包装内容中一样.

I know using background tint will work on this with same background color but my problem is I am using a selector on background. When selector is set to button background, it became wider. When I switched background to backgroundtint, the color become different (e.g. for me is violet) and it is not changing color on pressed. Padding are not working as work around by setting the negative values nor positive values. What else can I do ? My goal is my button will change color when clicked but it's height is narrow/default size like in wrap content.

这是我的按钮示例代码

<Button
    android:text="Add"
    android:fontFamily="@string/fontFamily"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:id="@+id/btnInformantsAdd"
    android:gravity="center"
    android:textColor="@color/white"
    android:textSize="@dimen/textSizeCommon"
    android:background="@drawable/ButtonSelectorBlue"
    android:paddingTop="-10dp"
    android:paddingBottom="-10dp"
    android:layout_gravity="center"
    android:layout_marginTop="@dimen/marginTop1"
    android:layout_marginBottom="@dimen/marginTop1" />

选择器XML代码

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="true">
    <layer-list>
      <item>
        <shape android:shape="rectangle">
          <solid android:color="@color/bluegreendark_light" />
          <padding android:left="10dp"
             android:top="@dimen/paddingSide1"
             android:right="10dp"
             android:bottom="@dimen/paddingSide1"/>
          <corners android:radius="5dp" />
        </shape>
      </item>
    </layer-list>
  </item>

  <item android:state_pressed="false">
    <layer-list>
      <item>
        <shape android:shape="rectangle">
          <solid android:color="@color/bluegreendark"/>
          <padding android:left="0dp"
             android:top="0dp"
             android:right="0dp"
             android:bottom="0dp"/>
          <corners android:radius="5dp" />
        </shape>
      </item>
    </layer-list>
  </item>
</selector>

推荐答案

设置背景后按钮变大

Button gets bigger when background is set

按钮没有变大.

这是按钮的默认背景,您可以看到它周围是透明的颜色.

Here is the button's default background, you can see that around it is a transparent color.

但是,当您将按钮的背景更改为您的 ButtonSelectorBlue 文件时,没有透明颜色,因此您认为它会变大.

But when you change the button's background to your ButtonSelectorBlue file, there is no transparent color, so it makes you think it gets bigger.

您可以看到以下代码:

<Button
    android:text="Add"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:id="@+id/btnInformantsAdd"
    android:gravity="center"
    android:textColor="#e7dbdb"
    android:textSize="20dp"
    android:background="@drawable/button_selector_blue"
    android:paddingTop="-10dp"
    android:paddingBottom="-10dp"
    android:layout_gravity="center"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp" />

<Button
    android:text="Add"
    android:id="@+id/bt"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:gravity="center"
    android:textColor="#282828"
    android:textSize="20dp"
    android:onClick="OnClick"
    android:layout_gravity="center" />

两个按钮,第一个背景是您的 ButtonSelectorBlue 文件,第二个是默认背景,并看到以下图片:

Two buttons, first background is your ButtonSelectorBlue file, second is default, and see this picture:

它们的大小相同.

因此,您需要在选择器文件的外围添加透明颜色,如下所示:

So, you need add transparent color in the your selector file's periphery, like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="true">
    <layer-list>
      <item>
        <shape android:shape="rectangle">
          <solid android:color="@color/bluegreendark_light" />
          <stroke android:color="#00000000"
                  android:width="10dp"/>
          <padding android:left="10dp"
             android:top="@dimen/paddingSide1"
             android:right="10dp"
             android:bottom="@dimen/paddingSide1"/>
          <corners android:radius="5dp" />
        </shape>
      </item>
    </layer-list>
  </item>

  <item android:state_pressed="false">
    <layer-list>
      <item>
        <shape android:shape="rectangle">
          <solid android:color="@color/bluegreendark"/>
          <stroke android:color="#00000000"
                  android:width="10dp"/>
          <padding android:left="0dp"
             android:top="0dp"
             android:right="0dp"
             android:bottom="0dp"/>
          <corners android:radius="5dp" />
        </shape>
      </item>
    </layer-list>
  </item>
</selector>

最后,预览:

结果:

这篇关于设置背景时按钮会变大-如何使其变小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆