在Toast中使两行文字居中的方法取决于所使用的开发框架和平台。以下是一种常见的实现方式:
示例代码如下:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout, findViewById(R.id.toast_root));
TextView text1 = layout.findViewById(R.id.text1);
TextView text2 = layout.findViewById(R.id.text2);
text1.setText("第一行文字");
text2.setText("第二行文字");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();
示例代码如下:
let toastView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 80))
let label1 = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 40))
let label2 = UILabel(frame: CGRect(x: 0, y: 40, width: 200, height: 40))
label1.text = "第一行文字"
label2.text = "第二行文字"
label1.textAlignment = .center
label2.textAlignment = .center
toastView.addSubview(label1)
toastView.addSubview(label2)
toastView.center = view.center
toastView.backgroundColor = UIColor.black.withAlphaComponent(0.6)
toastView.layer.cornerRadius = 10
view.addSubview(toastView)
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
toastView.removeFromSuperview()
}
以上是一种常见的实现方式,具体实现方法可能因开发框架和平台而异。
领取专属 10元无门槛券
手把手带您无忧上云