分享

xml中,button改变背景颜色方法

 quasiceo 2015-12-30

xml中,button改变背景颜色方法

在画几个设置界面,用到了button控件,对于button空间的背景色在不同状态下的颜色改变方法,做了一下尝试,发现了两种背景颜色改变的方法,就总结了下。

方法一尝试了好多遍才好,要点在于,在selector中android:drawable="@drawable/button_focus"引号中为xml文件,此xml文件为color类型,且在此color xml文件中

<color xmlns:android="http://schemas./apk/res/android"

   

    android:color="@color/button_focus_color">  <!-- 注意此处android:color的位置 -->

   

</color>

android:color="@color/button_focus_color"在color控件中。

 

方法一:填充button背景颜色的方法

       

在factory_reset这个xml文件中,其具体xml文件为:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas./apk/res/android"

   

    android:layout_width="560px"

    android:layout_height="348px"

    android:background="#212121"

         android:orientation="vertical"

        android:layout_gravity="center_vertical|center_horizontal">

   

        <LinearLayout

            android:layout_width="match_parent"

            android:layout_height="wrap_content" >   <!-- 怎样设置 -->

           

            <TextView

                android:layout_width="match_parent"

                android:layout_height="wrap_content"

                android:gravity="center|bottom"

                android:text="确定要恢复出厂设置吗?"

                android:textColor="#e6e6e6"

                android:textSize="34px"

                android:paddingTop="68px"

                />

           

        </LinearLayout>

       

        <LinearLayout

             android:layout_width="match_parent"

             android:layout_height="match_parent"

             android:orientation="vertical"

             android:gravity="center_horizontal" >

           

        <Button

                android:id="@+id/bn1"

                android:layout_width="520px"

                android:layout_height="72px"

                android:text="保存"

                android:textSize="28px"

                android:gravity="center_vertical|center_horizontal"

                android:layout_marginBottom="18px"

                android:layout_marginTop="60px"

                android:background="@drawable/button_background_selector"           

                android:textColor="@drawable/button_text_selector"

                />

            <Button

                android:id="@+id/bn2"

                android:layout_width="520px"

                android:layout_height="72px"        

                android:text="取消"

                android:textSize="28px"

                android:gravity="center_vertical|center_horizontal"

                android:background="@drawable/button_background_selector"

                android:textColor="@drawable/button_text_selector"

                />     

            

        </LinearLayout>

 

</LinearLayout>

 

其中的Button,以第一个为例:

 

    <Button

                android:id="@+id/bn1"

                android:layout_width="520px"

                android:layout_height="72px"

                android:text="保存"

                android:textSize="28px"

                android:gravity="center_vertical|center_horizontal"

                android:layout_marginBottom="18px"

                android:layout_marginTop="60px"

                android:background="@drawable/button_background_selector"           

                android:textColor="@drawable/button_text_selector"

                />

其中button_background_selectorxml文件,可在res中新建drawable文件夹并将其放置到其中,具体为

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas./apk/res/android" >

   

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

    <item android:drawable="@drawable/button_default" > </item>

 

</selector>

其中button_focus以及button_default也分别为xml文件,放在drawalbe文件夹中

button_focus.xmlxml文件具体为:

<?xml version="1.0" encoding="utf-8"?>

<color xmlns:android="http://schemas./apk/res/android"

   

    android:color="@color/button_focus_color">

   

</color>

 

button_default.xmlxml文件具体为:

<?xml version="1.0" encoding="utf-8"?>

<color xmlns:android="http://schemas./apk/res/android"

   

    android:color="@color/button_default_color">

   

</color>

 

其中的button_focus_colorbutton_default_colorvalues文件夹中新建的color.xml文件中定义的,具体代码如下:

 

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <color name="button_focus_color">#004B64</color>

    <color name="button_default_color">#3B3B3B</color>

    <color name="text_focus_color">#ffffff</color>

    <color name="text_default_color">#e6e6e6</color>       

</resources>

 

 

方法二:采用9patch图片做button背景图片的方法

在factory_reset这个xml文件中,其具体xml文件为:(跟方法一中的代码是一样的,方法二只是改变了button_background_selector这个xml文件里的东西

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas./apk/res/android"

   

    android:layout_width="560px"

    android:layout_height="348px"

    android:background="#212121"

         android:orientation="vertical"

        android:layout_gravity="center_vertical|center_horizontal">

   

        <LinearLayout

            android:layout_width="match_parent"

            android:layout_height="wrap_content" >   <!-- 怎样设置 -->

           

            <TextView

                android:layout_width="match_parent"

                android:layout_height="wrap_content"

                android:gravity="center|bottom"

                android:text="确定要恢复出厂设置吗?"

                android:textColor="#e6e6e6"

                android:textSize="34px"

                android:paddingTop="68px"

                />

           

        </LinearLayout>

       

        <LinearLayout

             android:layout_width="match_parent"

             android:layout_height="match_parent"

             android:orientation="vertical"

             android:gravity="center_horizontal" >

           

        <Button

                android:id="@+id/bn1"

                android:layout_width="520px"

                android:layout_height="72px"

                android:text="保存"

                android:textSize="28px"

                android:gravity="center_vertical|center_horizontal"

                android:layout_marginBottom="18px"

                android:layout_marginTop="60px"

                android:background="@drawable/button_background_selector"           

                android:textColor="@drawable/button_text_selector"

                />

            <Button

                android:id="@+id/bn2"

                android:layout_width="520px"

                android:layout_height="72px"        

                android:text="取消"

                android:textSize="28px"

                android:gravity="center_vertical|center_horizontal"

                android:background="@drawable/button_background_selector"

                android:textColor="@drawable/button_text_selector"

                />     

            

        </LinearLayout>

 

</LinearLayout>

 

 

其中的Button,以第一个为例:

 

    <Button

                android:id="@+id/bn1"

                android:layout_width="520px"

                android:layout_height="72px"

                android:text="保存"

                android:textSize="28px"

                android:gravity="center_vertical|center_horizontal"

                android:layout_marginBottom="18px"

                android:layout_marginTop="60px"

                android:background="@drawable/button_background_selector"           

                android:textColor="@drawable/button_text_selector"

                />

其中button_background_selectorxml文件,可在res中新建drawable文件夹并将其放置到其中,具体为:

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas./apk/res/android" >

   

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

    <item android:drawable="@drawable/button_normal" > </item>

 

</selector>

由于这里给出了button_pressedbutton_normal这两个9patch背景图片,所以可以直接用android:drawable=“两张9patch图片的位置”来改变button的背景。

         其中在res中新建了drawable文件夹,并在里边放了button_pressedbutton_normal这两个9patch图片,如下图所示:

 

                         

button_normal.9.png                           button_pressed.9.png

posted @ 2014-11-10 21:06 StearArre 阅读(5279) 评论(1) 编辑 收藏

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多