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圖片,如下圖所示:

 

                         

button_normal.9.png                           button_pressed.9.png


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM