使提示框居中显示&自定义提示框
ToastActivity.java文件:
1 public class ToastActivity extends AppCompatActivity { 2 private Button mbtnToast1,mbtnToast2,mbtnToast3; 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_toast); 7 mbtnToast1=findViewById(R.id.btn_toast1); 8 mbtnToast2=findViewById(R.id.btn_toast2); 9 mbtnToast3=findViewById(R.id.btn_toast3); 10 OnClick onClick=new OnClick(); 11 mbtnToast1.setOnClickListener(onClick); 12 mbtnToast2.setOnClickListener(onClick); 13 mbtnToast3.setOnClickListener(onClick); 14 } 15 16 class OnClick implements View.OnClickListener{ 17 18 @Override 19 public void onClick(View view) { 20 switch (view.getId()){ 21 case R.id.btn_toast1: 22 Toast.makeText(getApplicationContext(), "点击一", Toast.LENGTH_SHORT).show(); 23 break; 24 case R.id.btn_toast2: 25 Toast toastCenter=Toast.makeText(getApplicationContext(),"点击二居中显示",Toast.LENGTH_SHORT); 26 toastCenter.setGravity(Gravity.CENTER,0,0); 27 toastCenter.show(); 28 break;//使提示框居中显示 29 case R.id.btn_toast3: 30 Toast toastCustom=new Toast(getApplicationContext()); 31 LayoutInflater inflater=LayoutInflater.from(ToastActivity.this); 32 View view1 = inflater.inflate(R.layout.layout_toast, null); 33 ImageView imageView=view1.findViewById(R.id.iv_toast); 34 TextView textView=view1.findViewById(R.id.tv_toast); 35 imageView.setImageResource(R.drawable.zidingyi); 36 textView.setText("点击三自定义提示框出现"); 37 toastCustom.setView(view1); 38 toastCustom.setDuration(Toast.LENGTH_SHORT); 39 toastCustom.show(); 40 break; 41 } 42 } 43 } 44 }
然后对应activity_toast.xml文件:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:orientation="vertical"> 5 6 <Button 7 android:id="@+id/btn_toast1" 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content" 10 android:text="默认"/> 11 12 <Button 13 android:id="@+id/btn_toast2" 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:text="改变提示框出现位置"/> 17 18 <Button 19 android:id="@+id/btn_toast3" 20 android:layout_width="match_parent" 21 android:layout_height="wrap_content" 22 android:text="自定义(带图片)"/> 23 24 </LinearLayout>
还有一个layout_toast.xml文件来定义自定义提示框:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="wrap_content" 3 android:layout_height="wrap_content" 4 android:orientation="vertical" 5 android:background="@color/greydrak" 6 android:layout_gravity="center"> 7 <ImageView 8 android:id="@+id/iv_toast" 9 android:layout_width="100dp" 10 android:layout_height="100dp" 11 android:scaleType="fitCenter" 12 android:layout_gravity="center"/> 13 14 <TextView 15 android:id="@+id/tv_toast" 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 android:textSize="20dp" 19 android:textColor="@color/white" 20 android:layout_marginTop="5dp"/> 21 22 </LinearLayout>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义