简书链接:自定义toast的姿势以及解决设置marginpadding无效问题
文章字数:180,阅读全文大约需要1分钟
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable name="face" type="String" />
<variable name="nickname" type="String" />
<variable
name="self" type="com.buyao.tv.bean.YoungMyInfo" /> </data>
<FrameLayout android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content">
<LinearLayout android:id="@+id/view_wrap_innser" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@drawable/shape_young_toast_content" android:gravity="center_vertical" android:orientation="horizontal"
>
<FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content" >
<LinearLayout android:layout_width="wrap_content" android:layout_marginLeft="22dp" android:layout_height="wrap_content">
<cn.qssq666.rapiddevelopframe.ui.CircleImageView android:layout_width="36dp"
android:layout_height="36dp" android:layout_marginRight="4dp" android:scaleType="centerCrop" android:src="@drawable/young_img_defaultavata_white" app:face="@{face}" />
<View android:layout_width="4dp" android:layout_height="1dp" />
<cn.qssq666.rapiddevelopframe.ui.CircleImageView android:layout_width="36dp" android:layout_height="36dp" android:scaleType="centerCrop"
android:src="@drawable/young_img_defaultavata_white" app:face="@{self.face}" /> </LinearLayout>
<ImageView android:id="@+id/iv_love_state" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@drawable/youngquan_younghome_img_xiangshi" /> </FrameLayout>
<TextView android:id="@+id/tv_message"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:text="@string/loading" android:textColor="@color/colorWhite" />
</LinearLayout> </FrameLayout>
</layout>
|
首先shape防止在节点作为背景,
1 2 3 4 5
| 而我这里呢是在里面设置margin,其实也是一样的。
 代码如下
|
Toast toast = new Toast(context);
LayoutInflater inflater = LayoutInflater.from(context);
ViewYoungToastHaoganChangeBinding binding = DataBindingUtil.inflate(inflater, R.layout.view_young_toast_haogan_change, null, false);
binding.setFace(face);
binding.tvMessage.setText(“你与” + nickname + “的关系已达到” + message);
binding.setSelf(YoungStatic.getYoungUserInfo());
toast.setView(binding.getRoot());
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();