今天的任务为美化BeatBox应用,代码如下:
list_item_sound.xml:
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <data> <variable name="viewModel" type="com.bignerdranch.android.beatbox.SoundViewModel"/> </data> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp"> <Button android:layout_width="100dp" android:layout_height="100dp" android:layout_gravity="center" android:onClick="@{() -> viewModel.onButtonClicked()}" android:text="@{viewModel.title}" tools:text="Sound name"/> </FrameLayout> </layout>
button_beat_box_normal.xml:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="@color/dark_blue"/> </shape>
styles.xml:
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/red</item> <item name="colorPrimaryDark">@color/dark_red</item> <item name="colorAccent">@color/gray</item> <item name="android:windowBackground">@color/soothing_blue</item> <item name="buttonStyle">@style/BeatBoxButton</item> </style> <style name="BeatBoxButton"> <item name="android:background">@drawable/button_beat_box</item> </style> </resources>
button_beat_box_pressed.xml:
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="@color/red"/> </shape> </item> <item> <shape android:shape="oval"> <stroke android:width="4dp" android:color="@color/dark_red"/> </shape> </item> </layer-list>
button_beat_box.xml:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/button_beat_box_pressed" android:state_pressed="true"/> <item android:drawable="@drawable/button_beat_box_normal"/> </selector>
截图如下: