自定义对话框角落不圆 - CardViewAndroid

时间:2017-05-01 09:30:11

标签: android android-layout android-cardview customdialog

我正在开发Android应用程序,在App中我创建了一个自定义对话框,我使用CardView创建了对话框的设计,但是我无法在对话框中创建圆角。

我已经访问了Stackoverflow上的很多帖子以获得帮助,但是没有一个是有帮助的。

这是我的代码:

  

SKCDialog.java

public class SKCDialog  extends Dialog implements android.view.View.OnClickListener {

    public Activity c;
    public Dialog d;
    public Button yes, no;
    RelativeLayout closeAcessCodeDialog ;
    private EditText accessCodeText;
    private Button verifyAccessCode;
    private ImageButton closeDialog;

    public SKCDialog(Activity a) {
        super(a);
        this.c = a;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.access_code_dialog);


        closeAcessCodeDialog = (RelativeLayout)findViewById(R.id.closeAcessCodeDialog) ;
        accessCodeText = (EditText)findViewById(R.id.accessCodeText) ;
        verifyAccessCode = (Button)findViewById(R.id.verifyAccessCode) ;
        closeDialog = (ImageButton)findViewById(R.id.closeDialog) ;


        closeAcessCodeDialog.setOnClickListener(this);
        verifyAccessCode.setOnClickListener(this);
        closeDialog.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.closeAcessCodeDialog:
                dismiss();
                break;

            case R.id.verifyAccessCode:

                dismiss();

                break;
            case R.id.closeDialog:

                dismiss();

                break;
            default:
                break;
        }
       // dismiss();
    }

    public void closeDialog(View v){

    }
}
  

access_code_dialog.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:cardCornerRadius="30dp"
    >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ececec"
        android:orientation="vertical"
        android:focusable="true"
        android:focusableInTouchMode="true"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Access Code"
            android:hint="Enter Your Access Code"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:textSize="18sp"
            android:textStyle="bold"
            />

        <RelativeLayout
            android:id="@+id/closeAcessCodeDialog"
            android:layout_height="40dp"
            android:layout_width="40dp"
            android:layout_alignParentRight="true"
            android:gravity="center"
            >
            <ImageButton
                android:id="@+id/closeDialog"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:background="@drawable/cross"
                />

        </RelativeLayout>

        <EditText
            android:id="@+id/accessCodeText"
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_marginRight="10dp"
            android:layout_marginLeft="10dp"
            android:layout_below="@+id/closeAcessCodeDialog"
            android:layout_marginTop="20dp"
            android:background="@drawable/round_corner_shape"
            />


        <Button
            android:id="@+id/verifyAccessCode"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:layout_below="@+id/accessCodeText"
            android:paddingLeft="25dp"
            android:paddingRight="25dp"
            android:layout_marginTop="20dp"
            android:text="Submit"
            android:background="#007AFF"
            android:textColor="#fff"
           android:layout_centerHorizontal="true"
            android:layout_marginBottom="10dp"
            />

    </RelativeLayout>

</android.support.v7.widget.CardView>

我如何称呼SKCDialog:

SKCDialog cdd=new SKCDialog(screen);
cdd.setCancelable(false);
cdd.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
cdd.show();

请告诉我我做错了什么。

2 个答案:

答案 0 :(得分:0)

为圆形

创建可绘制文件
<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle" android:padding="15dp">

<solid android:color="#9CD6CB"/>
<corners
    android:bottomRightRadius="15dp"
    android:bottomLeftRadius="15dp"
    android:topLeftRadius="15dp"
    android:topRightRadius="15dp"/>
</shape>

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardCornerRadius="15dp">

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="true"
    android:background="@drawable/bg_rounded_button"
    android:focusableInTouchMode="true">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:hint="Enter Your Access Code"
        android:text="Access Code"
        android:textSize="18sp"
        android:textStyle="bold" />

    <RelativeLayout
        android:id="@+id/closeAcessCodeDialog"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:gravity="center">

        <ImageButton
            android:id="@+id/closeDialog"
            android:layout_width="25dp"
            android:layout_height="25dp" />

    </RelativeLayout>

    <EditText
        android:id="@+id/accessCodeText"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:layout_below="@+id/closeAcessCodeDialog"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="20dp" />


    <Button
        android:id="@+id/verifyAccessCode"
        android:layout_width="wrap_content"
        android:layout_height="35dp"
        android:layout_below="@+id/accessCodeText"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="20dp"
        android:background="#007AFF"
        android:paddingLeft="25dp"
        android:paddingRight="25dp"
        android:text="Submit"
        android:textColor="#fff" />

</RelativeLayout>

</android.support.v7.widget.CardView> 

答案 1 :(得分:0)

试试这个:

删除

android:background="#ececec"

来自相对布局和 添加

app:cardBackgroundColor="#ececec" 
在cardview中