【问题标题】:Specify Button's Background Shape Color Outside of the Background's XML在背景的 XML 之外指定按钮的背景形状颜色
【发布时间】:2015-12-12 02:57:14
【问题描述】:

我有一个Button,它使用一个形状作为背景。

形状会根据stateButton有不同的颜色

我可以在Button 的xml 中指定每个state 的颜色吗?对于新手的问题,我很抱歉。 请问有什么指南吗?

button_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" >
        <shape android:shape="rectangle"  >
            <corners android:radius="10dp" />
            <solid android:color="@color/colorPrimary" />
        </shape>
    </item>
    <item android:state_focused="true">
        <shape android:shape="rectangle"  >
            <corners android:radius="10dp" />
            <solid android:color="@color/colorPrimary" />
        </shape>
    </item>
    <item >
        <shape android:shape="rectangle"  >
            <corners android:radius="10dp" />
            <solid android:color="@color/colorWhite" />
        </shape>
    </item>
</selector>

目前如何称呼

    <Button
        android:id="@+id/loginButton"
        android:background="@drawable/button_background"
        android:text="@string/login_button_title"/>

【问题讨论】:

    标签: android android-view android-drawable android-button android-shape


    【解决方案1】:

    这个回答有点晚了,不过我在this post中详细描述了你想要什么,甚至如何在你的背景中产生涟漪,你可能想看看

    【讨论】:

      【解决方案2】:

      在 XML 中创建颜色状态列表。

      res/color/button_bg.xml:

      <selector xmlns:android="...">
          <item android:color="@color/colorPrimary" android:state_focused="true" />
          <item android:color="@color/colorPrimary" android:state_pressed="true" />
          <item android:color="@color/colorWhite" />
      </selector>
      

      那么你的 shape drawable 就可以简单地使用这个颜色资源了:

      res/drawable/button_bg.xml

      <shape xmlns:android="..."
          android:shape="rectangle"  >
      
          <corners android:radius="10dp" />
          <solid android:color="@color/button_bg" />
      </shape>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-27
        • 2013-06-24
        • 2020-11-11
        • 1970-01-01
        • 1970-01-01
        • 2020-08-19
        • 2013-08-06
        相关资源
        最近更新 更多