我正在开发一个 Android 应用程序。在我的应用程序中,我正在打开一个对话框之类的 Activity 。对于该 Activity 背景,我想为整个背景设置图像,但要有边框半径。所以我创建了这样的背景 XML 资源。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/match_background_image" />
<item>
<shape android:shape="rectangle" android:padding="10dp">
<corners
android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp"/>
</shape>
</item>
</layer-list>
我将该资源设置为 LinearLayout 的背景。当我打开 Activity 时,我得到这样的东西:
如您所见,拐角处没有边框半径。此外,我想做的是我还想将图像的 scaleType 设置为 cropCenter。那么是否可以仅在 XML 资源中完成?
最佳答案
我可以给你一个更好的方法,我已经用这个作为替代方法。您可以在 android 中使用 cardView 使您的角具有圆形。
在您的 gradle 依赖项中添加 cardView,
compile 'com.android.support:cardview-v7:25.1.1'
在您的 Activity 中需要作为对话框打开的,如下更改您的 xml,
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_dialog"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardCornerRadius="10dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/YOUR_IMAGE"/>
</android.support.v7.widget.CardView>
如果您正在使用如下扩展 AppCompatActivity 的 Activity,
i.e. DialogActivity extends AppCompatActivity
您需要像下面这样在 android list 中更改您的 Activity 主题,
<activity android:name=".DialogActivity"
android:theme="@style/Theme.AppCompat.Dialog"></activity>
其他
<activity android:theme="@android:style/Theme.Dialog" />
haaaaa,困难的部分已经完成,所以现在你唯一需要做的就是打电话
startActivity(new Intent(this, DialogActivity.class));
关于android - 在 Android 中使用背景图像自定义 XML 可绘制资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40482821/