Android Toast自訂

來源:互聯網
上載者:User

在Android項目開發中,經常會用到Toast這個控制項,但是系統的預設式樣太難看,有時需要改變一下,比如背景圖片,還有上面的提示文字,有的時候還需要動態改變提示的文字,比如顏色之類的

還有一個問題,在一個TextView上面,怎樣讓它顯示的內容有不同的顏色,比如“今天天氣好嗎?挺好的”,如果想讓“今天今天好嗎?”這一句顯示紅色,“挺好的”這三個字顯示綠色呢?

下面就兩個問題一併做解答,有圖有真相

接下來看代碼:

先是主布局檔案main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello"/>
<Button android:text="啟動Toast" android:id="@+id/button1"
android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
</LinearLayout>

相應的Activity

package com.toast;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MyToastActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new MyOnclickListener());
}
private class MyOnclickListener implements OnClickListener{

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
LayoutInflater inflater = LayoutInflater.from(MyToastActivity.this);
View view = inflater.inflate(R.layout.my_toast, (ViewGroup)findViewById(R.id.toast_layout_root));

TextView textView = (TextView) view.findViewById(R.id.text);

SpannableString ss = new SpannableString("今天天氣好嗎?挺好的");
ss.setSpan(new ForegroundColorSpan(Color.RED), 0, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new ForegroundColorSpan(Color.GREEN), 7, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(ss);

Toast toast = new Toast(MyToastActivity.this);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(view);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}

}
}

這裡注意紅色加粗的部分,就是處理在一個TextView中顯示的文字有不同的顏色的,當然還可以有刪除線,底線這些。。。

接下來就是Toast所用到的布局檔案了my_toast.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:background="@drawable/pop_select_tost"
>
<TextView android:id="@+id/text"
android:layout_width="200dip"
android:layout_height="fill_parent"
android:layout_marginTop="10dip"
android:textColor="@color/black"
android:layout_marginLeft="10dip"/>
</LinearLayout>

為了便於大家測試,一併把所用到的圖片也是附上吧

OK,到此

 

另外,如果不用SpannableString還可以用Html也可以

textView.setText(Html.fromHtml("<font size=\"3\" color=\"red\">今天天氣好嗎?</font><font size=\"3\" color=\"green\">挺好的</font>"));

一樣的效果,喜歡哪種用哪種吧

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.