0
点赞
收藏
分享

微信扫一扫

android 自定义按钮样式

修炼之士 2023-05-10 阅读 81


android 自定义按钮样式_xml

Blue button

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


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


                  <         item          android:state_pressed         =         "true"          >        


                  <         shape         >        


                  <         solid        


                  android:color         =         "#449def"          />        


                  <         stroke        


                  android:width         =         "1dp"        


                  android:color         =         "#2f6699"          />        


                  <         corners        


                  android:radius         =         "3dp"          />        


                  <         padding        


                  android:left         =         "10dp"        


                  android:top         =         "10dp"        


                  android:right         =         "10dp"        


                  android:bottom         =         "10dp"          />        


                  </         shape         >        


                  </         item         >        


                  <         item         >        


                  <         shape         >        


                  <         gradient        


                  android:startColor         =         "#449def"        


                  android:endColor         =         "#2f6699"        


                  android:angle         =         "270"          />        


                  <         stroke        


                  android:width         =         "1dp"        


                  android:color         =         "#2f6699"          />        


                  <         corners        


                  android:radius         =         "4dp"          />        


                  <         padding        


                  android:left         =         "10dp"        


                  android:top         =         "10dp"        


                  android:right         =         "10dp"        


                  android:bottom         =         "10dp"          />        


                  </         shape         >        


                  </         item         >        


         </         selector         >


Red button

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


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


                  <         item          android:state_pressed         =         "true"          >        


                  <         shape         >        


                  <         solid        


                  android:color         =         "#ef4444"          />        


                  <         stroke        


                  android:width         =         "1dp"        


                  android:color         =         "#992f2f"          />        


                  <         corners        


                  android:radius         =         "3dp"          />        


                  <         padding        


                  android:left         =         "10dp"        


                  android:top         =         "10dp"        


                  android:right         =         "10dp"        


                  android:bottom         =         "10dp"          />        


                  </         shape         >        


                  </         item         >        


                  <         item         >        


                  <         shape         >        


                  <         gradient        


                  android:startColor         =         "#ef4444"        


                  android:endColor         =         "#992f2f"        


                  android:angle         =         "270"          />        


                  <         stroke        


                  android:width         =         "1dp"        


                  android:color         =         "#992f2f"          />        


                  <         corners        


                  android:radius         =         "3dp"          />        


                  <         padding        


                  android:left         =         "10dp"        


                  android:top         =         "10dp"        


                  android:right         =         "10dp"        


                  android:bottom         =         "10dp"          />        


                  </         shape         >        


                  </         item         >        


         </         selector         >


Purple button

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


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


                  <         item          android:state_pressed         =         "true"          >        


                  <         shape         >        


                  <         solid        


                  android:color         =         "#a276eb"          />        


                  <         stroke        


                  android:width         =         "1dp"        


                  android:color         =         "#6a3ab2"          />        


                  <         corners        


                  android:radius         =         "3dp"          />        


                  <         padding        


                  android:left         =         "10dp"        


                  android:top         =         "10dp"        


                  android:right         =         "10dp"        


                  android:bottom         =         "10dp"          />        


                  </         shape         >        


                  </         item         >        


                  <         item         >        


                  <         shape         >        


                  <         gradient        


                  android:startColor         =         "#a276eb"        


                  android:endColor         =         "#6a3ab2"        


                  android:angle         =         "270"          />        


                  <         stroke        


                  android:width         =         "1dp"        


                  android:color         =         "#6a3ab2"          />        


                  <         corners        


                  android:radius         =         "4dp"          />        


                  <         padding        


                  android:left         =         "10dp"        


                  android:top         =         "10dp"        


                  android:right         =         "10dp"        


                  android:bottom         =         "10dp"          />        


                  </         shape         >        


                  </         item         >        


         </         selector         >


Green button

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


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


                  <         item          android:state_pressed         =         "true"          >        


                  <         shape         >        


                  <         solid        


                  android:color         =         "#70c656"          />        


                  <         stroke        


                  android:width         =         "1dp"        


                  android:color         =         "#53933f"          />        


                  <         corners        


                  android:radius         =         "3dp"          />        


                  <         padding        


                  android:left         =         "10dp"        


                  android:top         =         "10dp"        


                  android:right         =         "10dp"        


                  android:bottom         =         "10dp"          />        


                  </         shape         >        


                  </         item         >        


                  <         item         >        


                  <         shape         >        


                  <         gradient        


                  android:startColor         =         "#70c656"        


                  android:endColor         =         "#53933f"        


                  android:angle         =         "270"          />        


                  <         stroke        


                  android:width         =         "1dp"        


                  android:color         =         "#53933f"          />        


                  <         corners        


                  android:radius         =         "4dp"          />        


                  <         padding        


                  android:left         =         "10dp"        


                  android:top         =         "10dp"        


                  android:right         =         "10dp"        


                  android:bottom         =         "10dp"          />        


                  </         shape         >        


                  </         item         >        


         </         selector         >


Yellowbutton

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


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


                  <         item          android:state_pressed         =         "true"          >        


                  <         shape         >        


                  <         solid        


                  android:color         =         "#f3ae1b"          />        


                  <         stroke        


                  android:width         =         "1dp"        


                  android:color         =         "#bb6008"          />        


                  <         corners        


                  android:radius         =         "3dp"          />        


                  <         padding        


                  android:left         =         "10dp"        


                  android:top         =         "10dp"        


                  android:right         =         "10dp"        


                  android:bottom         =         "10dp"          />        


                  </         shape         >        


                  </         item         >        


                  <         item         >        


                  <         shape         >        


                  <         gradient        


                  android:startColor         =         "#f3ae1b"        


                  android:endColor         =         "#bb6008"        


                  android:angle         =         "270"          />        


                  <         stroke        


                  android:width         =         "1dp"        


                  android:color         =         "#bb6008"          />        


                  <         corners        


                  android:radius         =         "4dp"          />        


                  <         padding        


                  android:left         =         "10dp"        


                  android:top         =         "10dp"        


                  android:right         =         "10dp"        


                  android:bottom         =         "10dp"          />        


                  </         shape         >        


                  </         item         >        


         </         selector         >


Blackbutton

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


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


                  <         item          android:state_pressed         =         "true"          >        


                  <         shape         >        


                  <         solid        


                  android:color         =         "#343434"          />        


                  <         stroke        


                  android:width         =         "1dp"        


                  android:color         =         "#171717"          />        


                  <         corners        


                  android:radius         =         "3dp"          />        


                  <         padding        


                  android:left         =         "10dp"        


                  android:top         =         "10dp"        


                  android:right         =         "10dp"        


                  android:bottom         =         "10dp"          />        


                  </         shape         >        


                  </         item         >        


                  <         item         >        


                  <         shape         >        


                  <         gradient        


                  android:startColor         =         "#343434"        


                  android:endColor         =         "#171717"        


                  android:angle         =         "270"          />        


                  <         stroke        


                  android:width         =         "1dp"        


                  android:color         =         "#171717"          />        


                  <         corners        


                  android:radius         =         "4dp"          />        


                  <         padding        


                  android:left         =         "10dp"        


                  android:top         =         "10dp"        


                  android:right         =         "10dp"        


                  android:bottom         =         "10dp"          />        


                  </         shape         >        


                  </         item         >        


         </         selector         >



All text on the buttons will have the same styleso we can define a style in strings.xml:

<         style          name         =         "ButtonText"         >        


                  <         item          name         =         "android:layout_width"         >fill_parent</         item         >        


                  <         item          name         =         "android:layout_height"         >wrap_content</         item         >        


                  <         item          name         =         "android:textColor"         >#ffffff</         item         >        


                  <         item          name         =         "android:gravity"         >center</         item         >        


                  <         item          name         =         "android:layout_margin"         >3dp</         item         >        


                  <         item          name         =         "android:textSize"         >30dp</         item         >        


                  <         item          name         =         "android:textStyle"         >bold</         item         >        


                  <         item          name         =         "android:shadowColor"         >#000000</         item         >        


                  <         item          name         =         "android:shadowDx"         >1</         item         >        


                  <         item          name         =         "android:shadowDy"         >1</         item         >        


                  <         item          name         =         "android:shadowRadius"         >2</         item         >        


         </         style         >


调用 :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical">
   <Button android:text="Button" android:id="@+id/button1" android:background="@drawable/btn_red" style="@style/ButtonText"></Button>
   <Button android:text="Button" android:id="@+id/button2" android:background="@drawable/btn_blue" style="@style/ButtonText"></Button>
   <Button android:text="Button" android:id="@+id/button3" android:background="@drawable/btn_purple" style="@style/ButtonText"></Button>
   <Button android:text="Button" android:id="@+id/button4" android:background="@drawable/btn_green" style="@style/ButtonText"></Button>
   <Button android:text="Button" android:id="@+id/button5" android:background="@drawable/btn_orange" style="@style/ButtonText"></Button>
   <Button android:text="Button" android:id="@+id/button6" android:background="@drawable/btn_black" style="@style/ButtonText"></Button>
</LinearLayout>








举报

相关推荐

0 条评论