1. 程式人生 > >androidstudio去除Button自帶陰影效果

androidstudio去除Button自帶陰影效果

方法一:  直接在Button裡面設定style風格

關鍵程式碼:

style=”?android:attr/borderlessButtonStyle” 
  • 1
  • 1

具體配置:

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"             //點選事件的方法名
style="?android:attr/borderlessButtonStyle"
/>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

設定該屬性就可以去掉自帶的陰影。 
該屬性在API 11及以上可用。

方法二:封裝性增強

Android 5.0以後的版本中,定義一個button時,系統自動會加一個陰影的效果,有的時候這種效果看起來比較好,有的時候不符合UI的設計要求,這時候就需要手動去掉陰影。

網上很多文章寫了解決辦法,就是給button加一句話style="?Android:attr/borderlessButtonStyle",這個確實能解決問題,但是又帶來了另外一個問題,就是一般情況下,在寫佈局的時候,都會給每個控制元件寫一個style,這樣方便複用,比如我寫了一個button,引了一個style,但是這句話又得加一個style,這樣肯定就不行了,這時候有另外一個方法來解決,就是給button的style加一個parent

解決辦法:

  1. <Button
  2.         style="@style/Button_List_Style"
  3.         android:text="測試按鈕"/>

  1. <stylename="Button_List_Style"parent="@style/Widget.AppCompat.Button.Borderless">
  2.         <itemname="android:minWidth">100dp</item>
  3.         <itemname="android:minHeight">30dp</item
    >
  4.         <itemname="android:layout_width">wrap_content</item>
  5.         <itemname="android:layout_height">wrap_content</item>
  6.         <itemname="android:background">@drawable/btn_black_border_list</item>
  7.         <itemname="android:textSize">@dimen/text_size_small</item>
  8.         <itemname="android:textColor">@color/color_black</item>
  9. </style>

加上這句parent="@style/Widget.AppCompat.Button.Borderless"就可以了,這樣陰影就沒有了。