2012年3月13日星期二

實現透明的對話框

我們可以通過修改對話框佈局參數的 alpha 屬性, 實現透明的對話框.
WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.alpha = 0.6f;
dialog.getWindow().setAttributes(params);

透明的對話框

package com.TransparentDialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

public class TransparentDialogActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonStartDialog = (Button)findViewById(R.id.start);
buttonStartDialog.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
//Create AlertDialog
AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(TransparentDialogActivity.this);
myAlertDialog.setTitle("--- Title ---");

myAlertDialog.setMessage("Alert Dialog Message");

myAlertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
//...
}});

myAlertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
//...
}});

AlertDialog dialog = myAlertDialog.show();

WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.alpha = 0.6f;
dialog.getWindow().setAttributes(params);
}});
}

}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- Start -" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/ic_launcher"/>

</LinearLayout>

沒有留言:

發佈留言