android rounded rectangle without background straight-edge rectangle
I'm trying to make a dialog that appears as a rounded rectangle. I'm doing this by specifying the following shape xml as the background for the dialo开发者_如何学JAVAg layout.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FFFFFF"/>
<corners android:radius="40dp"/>
<padding android:left="10dp"
android:right="10dp"
android:top="10dp"
android:bottom="10dp"/>
</shape>
The resulting dialog has rounded corners inside a black 90 degree rectangle. I want to know how I can get rid of the ninety degree rectangle that remains and show nothing but the rounded rectangle.
It seems like the rectangle template stays behind after I curve the corners:
Here's a link to a picture of the dialog my code's producing: http://img577.imageshack.us/img577/8292/photoon20110912at2032.jpg
Write your constructor as:
public CustomDialog(Context context, int theme) {
super(context, theme);
getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
}
Try this:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:shape="rectangle">
<solid android:color="#B7B7B7"/>
<corners android:bottomLeftRadius="8dip"
android:bottomRightRadius="8dip"
android:topLeftRadius="8dip"
android:topRightRadius="8dip"/>
</shape>
This will give you the required rounded rectangle without background:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="270"/>
<stroke
android:width="2dp"
android:color="#FFF"/>
<corners
android:radius="40dp"/>
</shape>
UPDATE for future reference (it was added as comments)
Context mContext = getApplicationContext();
Dialog dialog = new Dialog(mContext,android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(R.layout.custom_dialog); dialog.show();
I was shocked when I occured the same problem, the solution is also a little weird. Create your own custom drawable for eg see below.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="@color/text_highlight" />
<stroke
android:width="5dp"
android:color="@color/text_highlight" />
<corners android:radius="12dp" />
<stroke
android:width="1dp"
android:color="@android:color/transparent" />
</shape>
Add the following lines to your dialog:
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
// This is line that does all the magic
dialog.getWindow().setBackgroundDrawableResource(
R.drawable.dialog_rounded);
精彩评论