2016-11-08 131 views
1

我正在开发一个Android应用程序。在我的应用程序中,我打开了一个像对话框一样的活动。对于那个活动背景,我想设置整个背景的图像,但是具有边框半径。所以我创建了这样的背景XML资源。在Android中使用图像定制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的背景。当我打开活动中,我得到的是这样的:

enter image description here

正如你可以看到有在角落没有边界半径。此外,我想要做的是我也想要将scaleType设置为cropCenter作为图像。那么是否有可能只是在XML资源中完成?

回答

1

我可以给你一个更好的方法,我用这个作为替代。你可以在android中使用cardView来创建一个圆形的角落。

添加cardView使用后在gradle这个依赖,

compile 'com.android.support:cardview-v7:25.1.1' 

了您的活动需要作为打开一个对话框,修改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的活动,如下所示:

i.e. DialogActivity extends AppCompatActivity 

您需要更改您的活动主题的Android清单如下图所示,

<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));