zoukankan      html  css  js  c++  java
  • xml中设置button的背景颜色

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

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

    <color xmlns:android="http://schemas.android.com/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.android.com/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.android.com/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.android.com/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.android.com/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.android.com/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.android.com/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图片,如下图所示:

  • 相关阅读:
    [AGC019F] Yes or No
    [CF1063F]String Journey
    [Gym100490A] Approximation
    [ARC058C]Iroha and Haiku
    [互测题目]大括号树
    [UVA10859]放置街灯 Placing Lampposts
    PAT甲级1141PAT Ranking of Institutions
    PAT甲级1153Decode Registration Card of PAT
    使用Python语言通过PyQt5和socket实现UDP服务器
    数据结构-哈希
  • 原文地址:https://www.cnblogs.com/guolili/p/4546854.html
Copyright © 2011-2022 走看看