2013-12-11 36 views
4

我想畫一個完美的圓形按鈕。 我嘗試使用下面的代碼:如何繪製完美的圓形按鈕?

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval" > 
    <stroke 
     android:width="3dip" 
     android:color="#065a32" /> 
    <corners android:radius="10dip"/> 
     <solid android:color="#eaebec" /> 
</shape> 

但我只能畫一個橢圓形,即使我改變參數:

<stroke 
android:width="3dip"/> 
<corners android:radius="10dip"/> 

我的截圖:

enter image description here

我已經嘗試過本網站的不同鏈接,但沒有一個能滿足我的需求。

編輯:

我的getView()的代碼是:

public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 
     if (convertView == null) { 
      LayoutInflater vi = (LayoutInflater) mContext 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = vi.inflate(R.layout.calendar_item, null); 

     } 
view.setBackgroundResource(R.drawable.calendar_cell_clicked); 
} 
+1

可以在Photoshop畫一個圓的圖像,並設置爲背景button.Can根據屏幕大小改變圖像的分辨率。 –

回答

7

使用這種XML

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="oval" > 

    <solid 
     android:color="#eaebec" /> 
</shape> 

,並在佈局如果要設置背景使用

 android:layout_width="50dp" 
     android:layout_height="50dp" 

或任何您需要的圓圈大小。

+0

感謝您的回答,我嘗試了您的代碼,但仍未獲得完美的圈子。另外,我不能給我的觀點不斷的參數,即它會根據屏幕大小而變化。 –

+0

如果其根據屏幕大小而變化,則根據屏幕大小在dimens.xml中定義大小。 – rachit

+1

該解決方案太繁瑣,即取代android:layout_width =「wrap_content」 android:layout_height =「wrap_content」有一些價值。我不認爲這是一個完美的解決方案。你讓它太複雜。考慮如果我需要用不同的按鈕尺寸來做到這一點。 –

0

使用這種XML

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval"> 
    <solid android:color="#9F2200"/> 
    <stroke android:width="2dp" android:color="#fff" /> 
</shape> 

,並在佈局

android:background="@drawable/button" 
0

在你繪製文件夾中創建以下

circular_button.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval" 
    android:innerRadius="15dp" 
    android:thickness="10dp" 
    android:useLevel="false"> 

    <solid android:color="#ce2c2c" /> 

</shape> 

以下屬性添加到任何按鈕使其圓形

android:background="@drawable/circular_button" 

輸出

Output