Android-自定义圆角Dialog遇到的问题

来源:互联网 发布:淘宝买发票 搜索什么 编辑:程序博客网 时间:2024/05/17 08:34

一、自定义Dialg样式
layout.xml

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="278dp"    android:layout_height="107dp"    android:background="@android:color/transparent"    >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical"        android:background="@drawable/shape_buzaiguanzhu_background">        <LinearLayout            android:layout_width="match_parent"            android:layout_height="107dp"            android:orientation="vertical">            <TextView                android:id="@+id/title"                android:layout_width="match_parent"                android:layout_height="0dp"                android:layout_weight="3"                android:gravity="center"                android:text="确定不再关注此人?"                android:textSize="12sp"                android:textColor="#000"/>            <View                android:layout_width="match_parent"                android:layout_height="2px"                android:background="#cdced2"                />            <LinearLayout                android:layout_width="match_parent"                android:layout_height="0dp"                android:layout_weight="2"                android:orientation="horizontal">                <TextView                    android:id="@+id/cancel"                    android:layout_width="0dp"                    android:layout_height="match_parent"                    android:layout_weight="1"                    android:text="取消"                    android:textColor="#0076ff"                    android:gravity="center"/>                <View                    android:layout_width="2px"                    android:layout_height="match_parent"                    android:background="#cdced2"                    />                <TextView                    android:id="@+id/sure"                    android:layout_width="0dp"                    android:layout_height="match_parent"                    android:layout_weight="1"                    android:text="确定"                    android:textColor="#0076ff"                    android:gravity="center"/>            </LinearLayout>        </LinearLayout>    </LinearLayout></LinearLayout> 

shape:

<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">    <solid android:color="#E6FFFFFF" ></solid>    <corners android:radius="8dp" /></shape>

二、自定义Dialog:

  public class NoMoreFoucsDialog extends Dialog {    private Context context;    private ClickListenerInterface clickListenerInterface;    public interface ClickListenerInterface {        public void doConfirm();        public void doCancel();    }    public NoMoreFoucsDialog(Context context) {        super(context);        this.context = context;    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        init();    }    public void init() {        LayoutInflater inflater = LayoutInflater.from(context);        View view = inflater.inflate(R.layout.layout_buzaiguanzhu, null);        setContentView(view);        TextView tvTitle = (TextView) view.findViewById(R.id.title);        TextView tvConfirm = (TextView) view.findViewById(R.id.sure);        TextView tvCancel = (TextView) view.findViewById(R.id.cancel);        tvConfirm.setOnClickListener(new clickListener());        tvCancel.setOnClickListener(new clickListener());        Window window = getWindow();        WindowManager.LayoutParams params = window.getAttributes();        params.gravity = Gravity.CENTER;        window.setAttributes(params);    }    public void setClicklistener(ClickListenerInterface clickListenerInterface) {         this.clickListenerInterface = clickListenerInterface;     }    private class clickListener implements View.OnClickListener {       @Override       public void onClick(View v) {             // TODO Auto-generated method stub             int id = v.getId();             switch (id) {             case R.id.sure:                 clickListenerInterface.doConfirm();                 break;             case R.id.cancel:                 clickListenerInterface.doCancel();                 break;             }         }     }}  

三、遇到的问题:
这样的dialog没有圆角效果。

四、问题解决:
1、定义一个dialog的style:

       <style name="MyDialogStyle" parent="@android:style/Theme.Dialog" >        <item name="android:windowFrame">@null</item>        <item name="android:windowIsFloating">true</item>        <item name="android:windowIsTranslucent">true</item>        <item name="android:windowNoTitle">true</item>        <item name="android:background">@android:color/transparent</item>        <item name="android:windowBackground">@android:color/transparent</item>        <item name="android:backgroundDimEnabled">true</item>        <item name="android:backgroundDimAmount">0.6</item>    </style>

2、在构造方法上去调用父类的带有style的构造方法:

    public NoMoreFoucsDialog(Context context,int style) {        super(context,style);        this.context = context;    }

五、最终效果:
这里写图片描述

0 0
原创粉丝点击