您的位置:首页 > 其它

Toast

2015-08-30 22:38 281 查看
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义toast"/>
</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textview_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是标题"/>
<ImageView
android:id="@+id/imageview_toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/textview_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是内容"/>

</LinearLayout>


public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button mButton1;
private Button mButton2;

@Override
protected void onCreate(Bundle savedInstanceState)  {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton1= (Button) findViewById(R.id.button1);
mButton2= (Button) findViewById(R.id.button2);
mButton1.setOnClickListener(this);
mButton2.setOnClickListener(this);

}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button1:
Toast toast= Toast.makeText(getApplicationContext(), "我是Toast", Toast.LENGTH_LONG);
Spanned spanned= Html.fromHtml("我<img src=''/>是<img src=''/>一个<font color='#ff0000'>toas</font>", new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
Drawable drawable =getResources().getDrawable(R.mipmap.ic_launcher);
drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
return drawable;
}
},null);
toast.setText(spanned);
toast.setDuration(Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER|Gravity.BOTTOM,0,0);
toast.show();
break;
case R.id.button2:
Toast toast1=Toast.makeText(getApplicationContext(),"我是Toast", Toast.LENGTH_LONG);
LayoutInflater inflater=getLayoutInflater();
View view=inflater.inflate(R.layout.activity_toast,null);
toast1.setView(view);
toast1.setDuration(Toast.LENGTH_LONG);
toast1.show();
break;
default:
break;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: