Android中Toast樣式及自訂Toast

來源:互聯網
上載者:User

標籤:android開發   toast   

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout"
    android:layout_width="200dip"
    android:layout_height="fill_parent"
    android:background="#f0f0f0"
    android:orientation="vertical" >

    <TextView
        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" >
    </TextView>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#999999"
        android:orientation="horizontal" >

        <ImageView
            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" >
        </ImageView>

        <TextView
            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" >
        </TextView>
    </LinearLayout>

</LinearLayout>

--------------------------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;
}
}
}

Android中Toast樣式及自訂Toast

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.