自定义对话框角不是圆的 - CardView Android
Custom Dialog Corners are not round - CardViewAndroid
我正在开发一个 Android 应用程序,在应用程序中我创建了一个自定义对话框,我使用 CardView 创建了对话框的设计,但我无法制作对话框的圆角.
我访问了 Whosebug 上的许多帖子以寻求帮助,但其中 none 个很有帮助。
这是我的代码:
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();
请指出我做错了什么。
为回合
创建drawable文件
<?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>
试试这个:
删除
android:background="#ececec"
来自相对布局和
添加
app:cardBackgroundColor="#ececec"
卡片视图
我正在开发一个 Android 应用程序,在应用程序中我创建了一个自定义对话框,我使用 CardView 创建了对话框的设计,但我无法制作对话框的圆角.
我访问了 Whosebug 上的许多帖子以寻求帮助,但其中 none 个很有帮助。
这是我的代码:
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();
请指出我做错了什么。
为回合
创建drawable文件<?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>
试试这个:
删除
android:background="#ececec"
来自相对布局和 添加
app:cardBackgroundColor="#ececec"
卡片视图