Android自定义Alertdialog对话框实现

目标:

 

自定义android alertdialog对话框的布局文件,利用shape来实现。

activity的CusAlertDiaglogForXml的代码如下:

public class CusAlertDiaglogForXml extends Activity {
 private Button cus_alert_dialog01_btn;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.cus_alert_diaglog);
  cus_alert_dialog01_btn = (Button) findViewById(R.id.cus_alert_dialog01_btn);
  cus_alert_dialog01_btn.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub

//自定义alertdialog的布局文件
    final AlertDialog cus_alertdialog01 = new AlertDialog.Builder(
      CusAlertDiaglogForXml.this).create();
    cus_alertdialog01.show();
    cus_alertdialog01.setCancelable(false);
    cus_alertdialog01.getWindow().setContentView(
      R.layout.cus_alertdiaglog_01_model);
    //修改alertdialog的标题和显示内容
    TextView alert_01_title = (TextView) cus_alertdialog01
      .getWindow().findViewById(R.id.alert_01_title);
    alert_01_title.setText("提示信息标题");

    TextView alert_01_content = (TextView) cus_alertdialog01
      .getWindow().findViewById(R.id.alert_01_content);
    alert_01_content.setText("这里是显示内容,按确定或取消按钮此警示框消失!");
    //添加按钮的处理事件
    cus_alertdialog01.getWindow().findViewById(R.id.alert_01_yes)
      .setOnClickListener(new OnClickListener() {

       @Override
       public void onClick(View v) {
        // TODO Auto-generated method stub
        cus_alertdialog01.dismiss();

       }
      });
    cus_alertdialog01.getWindow().findViewById(R.id.alert_01_no)
      .setOnClickListener(new OnClickListener() {

       @Override
       public void onClick(View v) {
        // TODO Auto-generated method stub
        cus_alertdialog01.dismiss();

       }
      });

   }
  });
 }

 

}

 

activity的布局文件cus_alert_diaglog.xml:

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
   >

            android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="通过XML自定义的alertdialog"
        android:id="@+id/cus_alert_dialog01_btn"/>"

 

alertdialog的布局文件CusAlertDiaglogForXml.xml

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center_vertical"
    android:orientation="vertical" >

            android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/table_frame_gray"
        android:orientation="vertical" >

                    android:id="@+id/alert_01_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="12dip"
            android:gravity="center"
            android:textSize="16sp"
            android:text="警示" />

                    android:layout_width="match_parent"
            android:layout_height="1dip"
            android:background="#4400ff00" />

                    android:id="@+id/alert_01_content"
            android:layout_gravity="center"
            android:layout_width="280sp"
            android:layout_height="wrap_content"
            android:layout_marginTop="12dip"
            android:layout_marginBottom="12dip"
            android:padding="8dip"
            android:textSize="12sp"
            android:text="警示内容" />

                    android:layout_width="match_parent"
            android:layout_height="1dip"
            android:background="#4400ff00" />

                    android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="horizontal" >

                            android:id="@+id/alert_01_yes"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/button_style"
                android:layout_marginLeft="14dip"
                android:layout_marginRight="14dip"
                                android:layout_marginTop="12dip"
                android:layout_marginBottom="14dip"
                android:text="确定"
                android:textSize="12sp" />

                            android:id="@+id/alert_01_no"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/button_style"
                android:layout_marginLeft="14dip"
                android:layout_marginRight="14dip"
                 android:layout_marginTop="12dip"
                android:layout_marginBottom="14dip"
                android:text="取消"
                android:textSize="12sp" />
       
   

 

alertdialog的linearylayout的样式文件table_frame_gray.xml:


    android:shape="rectangle" >

                    android:angle="180" />
    

   
   
   

 

按钮使用的样式文件button_style.xml.



   
       
                            android:angle="270" />
           
           
                            android:right="4dp" android:bottom="4dp" />
       

   

 
   
       
                            android:angle="270" />
           
           
                            android:right="4dp" android:bottom="4dp" />
       

   

 
   
       
                            android:angle="180" />
           
           
                            android:right="4dp" android:bottom="4dp" />
            
       

   

最后的效果如下:

Android自定义Alertdialog对话框实现_第1张图片

 

你可能感兴趣的:(Android学习分享)