android - 在 Android 中使用背景图像自定义 XML 可绘制资源

标签 android xml android-drawable xml-drawable

我正在开发一个 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 时,我得到这样的东西:

enter image description here

如您所见,拐角处没有边框半径。此外,我想做的是我还想将图像的 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/

相关文章:

android - 带有字母索引器的 Gridview 和在 android 中按字母顺序分组

android - 如何在android嵌入式卡中设置文本()

android - 在 android 中隐藏 Spinner 下拉图标

java - Android:无法将图像粘贴到 eclipse 中的drawable-hdpi 文件夹

android - 如何使用圆角 BitmapDrawable

android - 自定义 ProgressBar 旋转不流畅

java - 非法状态异常 : Fragment Already Added when resuming app

android - 在 Android 应用程序中有一个 "back"按钮是否合理?

c++ - 使用快速 xml 查找子元素

java - 简单 XML : ValueRequiredException