[英]How to set Style for a button in Android?
我在res文件夾中定義了以下文件。
styles_apptheme.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Generated with http://android-holo-colors.com -->
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="ButtonAppTheme" parent="android:Widget.Holo.Light.Button">
<item name="android:background">@drawable/apptheme_btn_default_holo_light</item>
</style>
</resources>
themes_apptheme.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Generated with http://android-holo-colors.com -->
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme" parent="@style/_AppTheme"/>
<style name="_AppTheme" parent="Theme.AppCompat.Light">
<item name="android:editTextBackground">@drawable/apptheme_edit_text_holo_light</item>
<item name="android:buttonStyle">@style/ButtonAppTheme</item>
</style>
</resources>
在我的Layout.xml文件中,我已經定義了我的按鈕,如下所示
<Button
android:id="@+id/btnAddTitle"
android:layout_below="@id/edEnterTitleValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_AddTitle"
android:layout_margin="20dp"
/>
如果我將以下行添加到上面的按鈕視圖,
android:background="@style/ButtonAppTheme"
應用程序崩潰說應該為background屬性設置drawable資源。
所以我創建了下面的drawable文件 - abc.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="@drawable/apptheme_btn_default_normal_holo_light" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="@drawable/apptheme_btn_default_disabled_holo_light" />
<item android:state_pressed="true"
android:drawable="@drawable/apptheme_btn_default_pressed_holo_light" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="@drawable/apptheme_btn_default_focused_holo_light" />
<item android:state_enabled="true"
android:drawable="@drawable/apptheme_btn_default_normal_holo_light" />
<item android:state_focused="true"
android:drawable="@drawable/apptheme_btn_default_disabled_focused_holo_light" />
<item
android:drawable="@drawable/apptheme_btn_default_disabled_holo_light" />
</selector>
如果我設置android:background="@drawable/abc"
我沒有看到按鈕中設置的樣式。
所以請告訴我如何設置按鈕的樣式。
謝謝。
如果你這樣做會很簡單。
首先在drawable文件夾中創建一個button_selector.xml。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
<shape android:shape="rectangle" >
<corners android:radius="5dp" />
<stroke android:width="1dip" android:color="@color/green_temp" />
<gradient android:angle="-90" android:startColor="@color/green_temp" android:endColor="@color/green_temp" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle" >
<corners android:radius="5dp" />
<stroke android:width="1dip" android:color="#971E05" />
<solid android:color="#58857e"/>
</shape>
</item>
<item >
<shape android:shape="rectangle" >
<corners android:radius="5dp" />
<stroke android:width="1dip" android:color="@color/bright_green" />
<gradient android:angle="-90" android:startColor="@color/green_temp" android:endColor="@color/button_green" />
</shape>
</item>
</selector>
在values文件夾的colors.xml中添加這些顏色。
<!-- Green -->
<color name="green_temp">#23A96E</color>
<color name="green_dark">#159204</color>
<color name="bright_green">#02D8B0</color>
<color name="button_green">#10a54a</color>
最后在你想要的layout.xml按鈕中放置選擇器的背景。
<Button
android:id="@+id/btnAddTitle"
android:layout_below="@id/edEnterTitleValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_AddTitle"
android:background="@drawable/button_selector"
android:layout_margin="20dp"
/>
然后完成所有按鈕將使用您想要的顏色設置樣式。
Android Button Maker是在線工具,可為Android Apps生成按鈕代碼。 Android API提供可繪制資源,其中XML文件定義幾何形狀,包括顏色,邊框和漸變。 這些按鈕基於形狀可繪制的XML代碼生成,與普通的png按鈕相比加載速度更快。 您可以在設置面板中自定義按鈕屬性並獲取源代碼。
檢查此鏈接Button Maker
無需破解大腦...這個工具使它變得簡單
更換
android:background="@style/ButtonAppTheme"
同
style="@style/ButtonAppTheme"
一切都好!
嘗試這個替代方案。 在res下創建一個drawable文件夾,然后在代碼下創建xml文件和copypaste,然后先嘗試根據您的要求進行自定義。
button_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#00acc1"/>
<stroke android:width="2dp" android:color="#ffffff"/>
<corners android:radius="2dp"/> </shape>
button_bg_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#006064"/>
<stroke android:width="1dp" android:color="#ffffff"/>
<corners android:radius="2dp"/> </shape>
button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:state_pressed="true"
android:drawable="@drawable/button_bg_pressed" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="@drawable/button_bg_pressed" />
<item android:drawable="@drawable/button_bg" /> </selector>
現在在你的按鈕xml中將背景設置為button_selector.xml
<Button
android:id="@+id/btnAddTitle"
android:layout_below="@id/edEnterTitleValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_AddTitle"
android:background="@drawable/button_selector"
android:layout_margin="20dp"/>
這將為您完成這項工作。 您可以通過這種方式自定義整個按鈕樣式。
如果您想通過材料設計獲得按鈕的默認動畫並更改按鈕顏色,請嘗試此操作。
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/padding_12"
android:text="button text here"
android:theme="@style/NavyBtn" />
在style.xml中
<style name="NavyBtn" parent="Theme.AppCompat.Light">
<item name="colorButtonNormal">#020e39</item>
<item name="android:textColor">#FFFFFF</item>
</style>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.