我需要创建一个活动,它应该看起来像带有圆角的对话框。
为了满足这个要求,我设置了
android:theme="@android:style/Theme.Dialog"
现在我的Activity看起来像一个对话框,但我需要它的边角是圆角的。
然后我创建了一个带有``属性的XML文件,并将其设置为我的Activity主题,但现在我的Activity看起来不像对话框。
请建议我该怎么做,以便我的Activity带有圆角的对话框外观。
我需要创建一个活动,它应该看起来像带有圆角的对话框。
为了满足这个要求,我设置了
android:theme="@android:style/Theme.Dialog"
现在我的Activity看起来像一个对话框,但我需要它的边角是圆角的。
然后我创建了一个带有``属性的XML文件,并将其设置为我的Activity主题,但现在我的Activity看起来不像对话框。
请建议我该怎么做,以便我的Activity带有圆角的对话框外观。
你可以制作一个拥有圆角的自定义主题theme
。首先,你需要为Activity
背景准备一个drawable
:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="15dp" />
<solid android:color="#565656" />
<stroke
android:width="3dp"
android:color="#ffffff" />
<padding
android:bottom="6dp"
android:left="6dp"
android:right="6dp"
android:top="3dp" />
</shape>
接下来,创建一个继承父级Theme.Dialog
的自定义主题:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="ThemeWithCorners" parent="android:Theme.Dialog">
<item name="android:windowBackground">@drawable/another_test_drawable</item>
</style>
</resources>
这将在名为styles.xml
的文件中,位于res/values
文件夹中。在Android清单中为您想要的Activity
使用此主题:
//...
<activity
android:name=".ActivityName"
android:label="@string/app_name"
android:theme="@style/ThemeWithCorners" >
//...
首先,创建一个圆角形状的可绘制对象,如下所示:
dialogbg.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="15dp" />
</shape>
然后,转到您活动的布局xml文件,并像这样更改其android:background属性
<RelativeLayout
android:layout_width="..."
android:layout_height="..."
android:background="@drawable/dialogbg">
<!--views here...-->
</RelativeLayout>
如果以上答案对任何人都不起作用,请尝试执行以下操作:
首先需要创建一个圆形,就像上面的任何示例一样:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="8dp" /> </shape>
1.将此形状设置为您的活动布局。 2.创建一个自定义样式,如下所示:
<style name="mycustomtheme" parent="android:Theme.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
</style>
`
现在在你的activity中设置主题
<activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/mycustomtheme" >