提示框的优化之自定义Toast组件之(一)Toast组件的布局实现

开发步骤: 

  • 在res下layout下创建一个Toast的布局资源文件toast_customer.xml 
  • 在最外层布局组件中为该布局添加android:id属性 
 1 //toast_customer.xml
 2 
 3 <?xml version="1.0" encoding="utf-8"?>
 4 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 5     android:id="@+id/toast_layout_root"  //在最外层布局组件中为该布局添加android:id属性
 6     android:layout_width="match_parent"
 7     android:layout_height="match_parent"
 8     android:orientation="horizontal"
 9 
10      <TextView
11         android:id="@+id/toastMessage"
12         android:layout_width="wrap_content"
13         android:layout_height="30dp"
14         android:gravity="center_horizontal|center_vertical"
15         android:textSize="13dp"
16         android:textColor="#000000"
17         />
18 </LinearLayout>
  • 在res下drawable下创建一个组件样式规则xml资源文件 
1 //toast_border_bg.xml
2 
3 <?xml version="1.0" encoding="utf-8"?>
4 <shape xmlns:android="http://schemas.android.com/apk/res/android" >
5     <corners android:radius="8dp"/>
6     <solid android:color="#55FFFFFF"/>
7     <stroke android:width="0.5dp" android:color="#FFFFFF"/>
8 </shape>
  • toast_customer.xml引用创建好的样式规则资源文件
 1 //toast_customer.xml
 2 
 3 <?xml version="1.0" encoding="utf-8"?>
 4 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 5     android:id="@+id/toast_layout_root"
 6     android:layout_width="match_parent"
 7     android:layout_height="match_parent"
 8     android:orientation="horizontal"
 9     android:background="@drawable/toast_border_bg">  //引用创建好的样式规则资源文件toast_border_bg.xml
10 
11     <TextView
12         android:id="@+id/toastMessage"
13         android:layout_width="wrap_content"
14         android:layout_height="30dp"
15         android:gravity="center_horizontal|center_vertical"
16         android:textSize="22dp"
17         android:text="测试"
18         android:textColor="#000000"
19         />
20 </LinearLayout>

运行:

 

转载于:https://www.cnblogs.com/zulo/p/5090633.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值