自定义view示例_Android按钮自定义示例

自定义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

输出量

Normal button: Android Button Customization Example

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

输出 -按钮名称将隐藏,并在按钮名称的位置显示图像

Button with Image: Android Button Customization Example

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

输出量

Button with color: Android Button Customization Example

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

输出量

Button with rounded corner: Android Button Customization Example

翻译自: https://www.includehelp.com/android/button-customization-example.aspx

自定义view示例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值