2016-11-08 129 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));