android自定义dialog弹出框、透明背景

Dialog是android开发过程中最常用到的组件之一,它包括以下几种类型:

  1. 警告对话框:Alertialog
  2. 进度对话框:ProgressDialog
  3. 日期选择对话框:DatePickerDialog
  4. 时间选择对话框:TimePickerDialog
  5. 自定义对话框:从Dialog继承

Dialog的创建方式有两种:

一是直接new一个Dialog对象,然后调用Dialog对象的show和dismiss方法来控制对话框的显示和隐藏。

二是在Activity的onCreateDialog(int id)方法中创建Dialog对象并返回,然后调用Activty的showDialog(int id)和dismissDialog(int id)来显示和隐藏对话框。

区别在于通过第二种方式创建的对话框会继承Activity的属性,比如获得Activity的menu事件等。

-------------------------------------------------------------------------------------------------------------------------------------------------------------------


CondimentDialog.java

public class CondimentDialog extends Dialog{

 private Context mContext;
 private Button okButton;
 
 public CondimentDialog(Context context, int theme) {
  super(context,theme);
  mContext = context;
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  setContentView(R.layout.dialog);
  
  
  LayoutParams params = getWindow().getAttributes(); 
  params.height = LayoutParams.WRAP_CONTENT;
  params.width = LayoutParams.WRAP_CONTENT;
  getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
 
  okButton = (Button)findViewById(R.id.btnOK);
  okButton.setText("shuosdhsfsdfsdf");
  okButton.setOnClickListener(new Button.OnClickListener(){

   public void onClick(View v) { 
   // TODO Auto-generated method stub 
    Log.e("dialog dismiss","dismiss+++++++++++++++++++++++++++"); 
        dismiss(); 
   
   } }
  ); 
   
 }
 
 protected void onStop() { 
  Log.e("dialog onStop","dialog onStop+++++++++++++++++++++++++++"); 
  }


}

TestAdroidtbActivity.java

 

public class TestAdroidtbActivity extends Activity {
    /** Called when the activity is first created. */
 
 private TextView textView;
 private Button button_ok;
 private Context mContext;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
      
        textView = (TextView)this.findViewById(R.id.itemName);
        button_ok = (Button)this.findViewById(R.id.btnOK);
        
        button_ok.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    Log.e("activity", "onclick....");  
    CondimentDialog dialog = new CondimentDialog(TestAdroidtbActivity.this,R.style.dialog);
    dialog.setCanceledOnTouchOutside(false);
    dialog.show();
   }
  });

    }

}

main.xml


 android:id="@+id/relmain"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:orientation="vertical"
    android:background="@color/tb">
 
    android:id="@+id/itemName"
     android:layout_height="wrap_content" 
  android:layout_width="fill_parent" android:textSize="20dp"
  android:gravity="center"
  android:layout_marginTop="50px"
  android:layout_marginLeft="8px"
  android:layout_marginRight="8px" 
  android:text="弹出对话框"/> 
  
       android:id="@+id/btnOK"
     android:text="ok"
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:gravity="center"
     android:layout_marginTop="80px"
  android:layout_marginLeft="15px"/>

dialog.xml 

 


  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="819px"
  android:layout_height="483px"
  android:background="@drawable/pig">
    
         android:id="@+id/btnOK"
     android:text="ok"
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_gravity="center_vertical"
  android:layout_marginTop="180px"
  android:layout_marginLeft="300px"/>


 styles.xml

 

 
    
   

color.xml



 #880000AA
 #FFF000
 #00FFFFFF

你可能感兴趣的:(Android)