自定义Dialog

工作中会大量的用到dialog,但是系统自带dialog不是很漂亮,大部分都是自定义dialog

1.自定义dialog布局.

<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="250dip"
    android:layout_height="200dip"
    android:background="@drawable/bg_dialog"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dip"
        android:layout_marginTop="5dip" >

        <ImageView
            android:layout_width="40dip"
            android:layout_height="40dip"
            android:layout_marginLeft="10dip"
            android:src="@drawable/ic_delete" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="警告"
            android:textColor="#FF0000"
            android:textSize="25sp" />
    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="确定删除该项"
            android:textColor="#000000"
            android:textSize="25sp" />
    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dip"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="10dip"
        android:layout_marginTop="5dip" >

        <Button
            android:id="@+id/btn_dialog_cancel"
            android:layout_width="100dip"
            android:layout_height="40dip"
            android:background="@drawable/bg_button"
            android:text="取消" />

        <Button
            android:id="@+id/btn_dialog_delete"
            android:layout_width="100dip"
            android:layout_height="40dip"
            android:layout_alignParentRight="true"
            android:background="@drawable/bg_button"
            android:text="删除" />
    </RelativeLayout>

</LinearLayout</span>>

           1.1 自定义圆角矩形背景

<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>  
<shape xmlns:android="http://schemas.android.com/apk/res/android"  
    android:shape="rectangle" >  
  
    <corners  
        android:bottomLeftRadius="8dp"  
        android:bottomRightRadius="8dp"  
        android:topLeftRadius="8dp"  
        android:topRightRadius="8dp" />  
     <!-- 填充的颜色 -->
    <solid android:color="#FFFFFF" />
  
    <padding  
        android:bottom="7dp"  
        android:left="7dp"  
        android:right="7dp"  
        android:top="7dp" />  
  
</shape>  </span>

    1.2自定义button背景

    

<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true"><!-- //定义当button 处于pressed 状态时的形态。 -->
                <shape>
                    <solid android:color="#008aed" />
                      <!-- <stroke   android:width="2dp" android:color="#000000" /> -->
                       <corners android:radius="8dp" />  
                       
                 </shape>
		</item>
		<item android:state_enabled="false"><!-- //定义当button获得 focus时的形态 -->
                 <shape>
                     <solid android:color="#a55bc0de" />
                        
                         <corners android:radius="8dp" />   
                                
                </shape>
	</item>
	<item>
        <shape>
            <solid android:color="#a55bc0de" />
            
            <corners android:radius="8dp" />   
             
        </shape>
     </item>
</selector></span>


2.自定义dialog

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MyDialog extends Dialog implements android.view.View.OnClickListener {
	 Context context;
	 private ClickListenerInterface clickListenerInterface;
	 public interface ClickListenerInterface {

			public void doConfirm();

			public void doCancel();
		}
	    public MyDialog(Context context) {
	        super(context);
	        this.context = context;
    }
	    public MyDialog(Context context, int theme){
	        super(context, theme);
	        this.context = context;
	    }
	    public void setClicklistener(ClickListenerInterface clickListenerInterface) {
			this.clickListenerInterface = clickListenerInterface;
		}
	    @Override
	    protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        this.setContentView(R.layout.delete_dialog);
	        Button btn_delete = (Button) findViewById(R.id.btn_dialog_delete);
			Button btn_cancel = (Button) findViewById(R.id.btn_dialog_cancel);

			btn_delete.setOnClickListener(this );
			btn_cancel.setOnClickListener(this);
	    }

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			int id = v.getId();
			switch (id) {
			case R.id.btn_dialog_delete:
				clickListenerInterface.doConfirm();
				break;
			case R.id.btn_dialog_cancel:
				clickListenerInterface.doCancel();
				break;
			}
		};
}

           2.1自定义stytle

            

<span style="font-size:14px;"><style name="MyDialog" >
        <item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@drawable/round_retangle</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:backgroundDimEnabled">false</item>
    </style></span>

  2.3 icon 系统自带

3调用自定义dialog

<span style="font-size:14px;">MyDialog dialog=new MyDialog(MainActivity.this, R.style.MyDialog);
					
					dialog.setClicklistener(new ClickListenerInterface() {
						
						@Override
						public void doConfirm() {
							Toast.makeText(MainActivity.this,"删除了" , 0).show();
							
						}
						
						@Override
						public void doCancel() {
							Toast.makeText(MainActivity.this,"不管了" , 0).show();
							
						}
					});
					dialog.show();
				}</span>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值