android中自定义 toast,Android中Toast样式及自定义Toast

android:id="@+id/toast_layout"

android:layout_width="200dip"

android:layout_height="fill_parent"

android:background="#f0f0f0"

android:orientation="vertical" >

android:id="@+id/txt_Title"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center|top"

android:text="@string/toast_text_1"

android:textColor="#ffffff"

android:textSize="20dip" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="#999999"

android:orientation="horizontal" >

android:id="@+id/image_toast"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginRight="10dip"

android:src="@drawable/icon_message_nub" >

android:id="@+id/txt_context"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center|right"

android:text="@string/toast_text_2"

android:textColor="#ffffff"

android:textSize="15dip" >

--------------------------Activity代码------------------------

public class ToastActivity extends Activity implements OnClickListener {

private Button style1btn;

private Button style2btn;

private Button style3btn;

private Button style4btn;

private Toast toast = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.test_main);

initViews();

initListeners();

}

private void initViews() {

style1btn = (Button) findViewById(R.id.style1_btn);

style2btn = (Button) findViewById(R.id.style2_btn);

style3btn = (Button) findViewById(R.id.style3_btn);

style4btn = (Button) findViewById(R.id.style4_btn);

}

private void initListeners() {

style1btn.setOnClickListener(this);

style2btn.setOnClickListener(this);

style3btn.setOnClickListener(this);

style4btn.setOnClickListener(this);

}

@Override

public void onClick(View v) {

AlertDialog.Builder builder;

AlertDialog dialog;

switch (v.getId()) {

case R.id.style1_btn:

Toast.makeText(ToastActivity.this, "系统默认的Toast样式", Toast.LENGTH_LONG).show();

break;

case R.id.style2_btn:

toast = Toast.makeText(ToastActivity.this, "改变Toast显示位置的样式", Toast.LENGTH_LONG);

/**设置显示位置为居中*/

toast.setGravity(Gravity.CENTER, 0, 0);

toast.show();

break;

case R.id.style3_btn:

toast = Toast.makeText(ToastActivity.this, "自定义带图片的Toast样式", Toast.LENGTH_LONG);

LayoutInflater inflater = getLayoutInflater();

/**自定义布局*/

View view = inflater.inflate(R.layout.my_custom_toast, (ViewGroup) findViewById(R.id.toast_layout));

toast = new Toast(getApplicationContext());

toast.setGravity(Gravity.CENTER, 0, 0);

toast.setDuration(Toast.LENGTH_LONG);

toast.setView(view);

toast.show();

break;

case R.id.style4_btn:

LayoutInflater inflater1 = getLayoutInflater();

View styleView = inflater1.inflate(R.layout.my_custom_toast, (ViewGroup) findViewById(R.id.toast_layout));

TextView tv = (TextView) styleView.findViewById(R.id.txt_context);

tv.setText("长时间显示的Toast就是自己定义的一个Dialog!");

builder = new AlertDialog.Builder(this);

builder.setView(styleView);

dialog = builder.create();

dialog.show();

break;

default:

break;

}

}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值