完全自定义dialog布局的实现

因为项目需要美工做出了dialog 的样式文字大小以及颜色。我试过一种是加载inflate的那个界面但是那个alertdialog不能自定义按钮和文字。而我需要把按钮和文字的样式以及大小自定义,而修改系统theme下的样式很麻烦,还有一些修改弹出框大小的属性不起作用,所以我就自定义一个alertdialog样式。
下面直接上代码
这个是主函数:

protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
        Button button = (Button)findViewById(R.id.btnread);
      button.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v)
         {
            DialogInterface.OnClickListener listener;
            listener = new DialogInterface.OnClickListener()
            {

                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    // TODO Auto-generated method stubf
                    finish();        
                }
            };

            final CharSequence msg = "数据";

           final AlertDialog builder = new AlertDialog.Builder(MainActivity.this)  
            .create();
            builder.show();
            builder.getWindow().setContentView(R.layout.dialog);//设置弹出框加载的布局

            TextView tv_title = (TextView) builder.findViewById(R.id.tv_dialog_title);  
            tv_title.setText(msg);

            builder.getWindow()  
            .findViewById(R.id.button1)  
            .setOnClickListener(new View.OnClickListener() {  //按钮点击事件
            @Override  
            public void onClick(View v) {  
                builder.dismiss();  
            }  
            });
         }

    });     
    }

他加载了一个界面布局,而这个布局是我自己定义的。你可以按照自己的需求在此xml中加入自己想加入的控件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:background="#ffffff"  
    android:orientation="vertical" >  

    <TextView  
        android:id="@+id/tv_dialog_title"  
        android:layout_width="match_parent"  
        android:layout_height="90dp" 
        android:gravity="center" 
        android:textColor="#2F2F2F"  
        android:textSize="24sp"  
         />
         //划一条分割线
        <View  
         android:layout_width="match_parent"  
         android:layout_height="1dp" 
         android:background="#d3d3d3"  />
    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="66dp"
        android:background="#ffffff"
        android:textColor="#2f2f2f"
        android:textSize="30sp"
        android:text="确定" />
LinearLayout>  

你可能感兴趣的:(Android)