自定义view示例
Button is a widget in android performs click event on the form i.e. for to submit the data in the database or to start/close some operations, button is being used. There are different types of button widgets available in android like Normal Button, Image Button, Radio Button, Check Boxes and Toggle Button.
Button是android中的一个小部件,用于在表单上执行click事件,即用于提交数据库中的数据或启动/关闭某些操作,正在使用button。 android中有不同类型的按钮小部件可用,例如“ 普通按钮” ,“ 图像按钮” ,“单选按钮”,“复选框”和“切换按钮”。
1)普通按钮 (1) Normal Button)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.faraz.buttoncustomization_example.MainActivity">
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="148dp"
tools:layout_editor_absoluteY="131dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="148dp"
tools:layout_editor_absoluteY="231dp" />
</android.support.constraint.ConstraintLayout>
Output
输出量
2)按钮背景中的图像 (2) Image in Background of button)
1) Add this line to old code
1)将此行添加到旧代码
android:background="@drawable/yourimage"
2) delete this line from old code
2)从旧代码中删除此行
android:text="Button"
Final Code:
最终代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.faraz.buttoncustomization_example.MainActivity">
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="148dp"
tools:layout_editor_absoluteY="131dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/your_image"
tools:layout_editor_absoluteX="148dp"
tools:layout_editor_absoluteY="231dp" />
</android.support.constraint.ConstraintLayout>
Output - The button name will hide and image will show in place of button name
输出 -按钮名称将隐藏,并在按钮名称的位置显示图像
3)更改按钮的颜色 (3) Changing the color of Button)
Shape is changed from rectangle to oval.
形状从矩形更改为椭圆形。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<size android:width="100dp"
android:height="100dp"/>
<solid android:color="#59d5fe"/>
</shape>
</item>
</selector>
Output
输出量
4)按钮角圆 (4) Button Corner Round)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#DEB887"/>
<stroke android:color="#8A2BE2" android:width="2dp" />
<corners android:radius="15dp" />
</shape>
</item>
</selector>
Output
输出量
翻译自: https://www.includehelp.com/android/button-customization-example.aspx
自定义view示例