There are times in the project that consider aesthetics and require a custom button.
1. Create a new folder in the Res folder of the project drawable and create a new shapes.xml (resource that implements the shape and color of the button)
2. Set android:background= "@drawable/shapes" in the button property of Mainactivity.xml.
Color changes for rounded buttons, no clicks, and clicks.
The effect is as follows:
Shapes.xml
<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android"> <!--the initialization color of the view click Gradient gradient android:angle gradient angle, 0 from top to bottom, 90 for left to right, 45 for integer times default to 0; Android:startcolor start color an Droid:endcolor End color Android:type gradient style liner linear gradient radial annular gradient sweep view sizes size Corners fillet - <Itemandroid:state_pressed= "true"> <Shape> <GradientAndroid:angle= "+"Android:endcolor= "#308014"Android:startcolor= "#308014" /> <sizeAndroid:height= "60DP"Android:width= "120DP" /> <CornersAndroid:radius= "8DP" /> </Shape> </Item> <!--Initialize color when view is not clicked - <Item> <Shape> <GradientAndroid:angle= "+"Android:endcolor= "#32CD32"Android:startcolor= "#32CD32" /> <sizeAndroid:height= "60DP"Android:width= "120DP" /> <CornersAndroid:radius= "8DP" /> </Shape> </Item></selector>
Mainactivity.xml
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context=". Mainactivity "> <TextView Android:id= "@+id/textview1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/hello_world"/> <Button Android:id= "@+id/stop"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignbaseline= "@+id/start"Android:layout_alignbottom= "@+id/start"Android:layout_marginleft= "30DP"Android:layout_torightof= "@+id/start"Android:text= "Stop"Android:background= "@drawable/shapes"/> <Button Android:id= "@+id/unbind"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignbaseline= "@+id/bind"Android:layout_alignbottom= "@+id/bind"Android:layout_alignleft= "@+id/stop"Android:text= "Unbind"Android:background= "@drawable/shapes"/> <Button Android:id= "@+id/start"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignleft= "@+id/textview1"Android:layout_below= "@+id/textview1"Android:layout_marginleft= "17DP"Android:layout_margintop= "108DP"Android:background= "@drawable/shapes"Android:text= "Start"Android:textcolor= "#FFFFFF"/> <Button Android:id= "@+id/bind"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignleft= "@+id/start"Android:layout_below= "@+id/start"Android:layout_margintop= "43DP"Android:background= "@drawable/shapes"Android:text= "Bind"/></relativelayout>
Implementing a custom button in Android