2011-04-29 43 views
7

我正在嘗試創建一個帶有圓角(以及選擇的背景顏色)的視圖,以便我可以重複使用不同的背景顏色;很難解釋,所以這裏是我的代碼:帶有圓角的Android自定義組件視圖

/app/src/com/packagename/whatever/CustomDrawableView.java


package com.packagename.whatever; 

import android.content.Context; 
import android.content.res.TypedArray; 
import android.graphics.Canvas; 
import android.graphics.drawable.PaintDrawable; 
import android.util.AttributeSet; 
import android.view.View; 

public class CustomDrawableView extends View { 
    private PaintDrawable mDrawable; 
    int radius; 

    private void init(AttributeSet attrs) { 
     TypedArray a = getContext().obtainStyledAttributes(attrs,R.styleable.RoundedRect); 
     radius = a.getInteger(R.styleable.RoundedRect_radius, 0); 
    } 

    public CustomDrawableView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(attrs); 

     mDrawable = new PaintDrawable(); 
    } 

    protected void onDraw(Canvas canvas) { 
     mDrawable.setCornerRadius(radius); 
     mDrawable.draw(canvas); 
    } 
} 

這裏顯示自定義組件的XML: /應用/ RES /佈局/的test.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ny="http://schemas.android.com/apk/res/com.packagename.whatever" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#ffffff" 
    android:padding="10dp"> 

    <com.packagename.whatever.CustomDrawableView 
     android:id="@+id/custom" 
     android:layout_width="200dp" 
     android:layout_height="200dp" 
     android:background="#b80010" 
     ny:radius="50" 
    /> 

</LinearLayout> 

我想紅盒子有50px的圓角,但正如你所看到的,它不會:

Red box without rounded corners

的想法是,我可以很容易改變背景顏色在XML和自動有一個漂亮的景觀帶圓角,而無需創建多個可繪製。

感謝您的幫助!

+0

試試這個http://www.gadgetsaint.com/tips/rounded-corners-views-layouts-android/#.WPz2QVN97BI – ASP 2017-04-23 18:53:04

回答

8

您需要將角落半徑和顏色設置爲背景可繪製。

這是一種可行的方法。獲取您在android:background中設置的顏色,然後使用它創建一個新的可繪製對象,並將其設置爲構造函數中的背景。只要你只將android:background設置爲一個顏色值就可以工作。

public CustomDrawableView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(attrs); 

     // pull out the background color 
     int color = attrs.getAttributeIntValue("http://schemas.android.com/apk/res/android", "background", 0xffffffff); 

     // create a new background drawable, set the color and radius and set it in place 
     mDrawable = new PaintDrawable(); 
     mDrawable.getPaint().setColor(color); 
     mDrawable.setCornerRadius(radius); 
     setBackgroundDrawable(mDrawable); 
    } 

如果您重寫onDraw,請確保先調用super.onDraw(canvas)以獲取繪製的背景。

+0

這樣做!謝謝!! – iamkoa 2011-04-29 02:20:29

2
+0

感謝您的迴應,但我試圖動態設置背景顏色。你的前兩個鏈接是我通常使用的方法,但只允許靜態BG,因此我的問題。第三個環節可能會訣竅...我會看看。 :) – iamkoa 2011-04-29 01:43:51

+0

@iamkoa之後調用'setBackgoundColor()'不起作用? – Aleadam 2011-04-29 01:48:00

+0

是的,但這需要您添加一個android:background drawable,然後手動覆蓋Java中drawable的背景顏色,對吧?我正在尋找一種解決方案,允許一切都在一個XML調用中。 – iamkoa 2011-04-29 01:58:17

3

給出一個簡單的shapedrawable這樣的:

public ShapeDrawable Sd(int s){ 

float[] outerR = new float[] { 12, 12, 12, 12, 12, 12, 12, 12 }; 
ShapeDrawable mDrawable = new ShapeDrawable(new RoundRectShape(outerR, null,null)); 

      mDrawable.getPaint().setColor(s); 
return mDrawable; 
} 

可以請執行以下操作:

LinearLayout l=(LinearLayout) findViewById(R.id.testLayout); 
l.setBackgroundDrawable(Sd(0xff74AC23)); 

其中12代表半徑。 你可以將它應用於任何視圖來繪製背景。

+0

如果你已經有顏色的按鈕,你需要圓角他們我使用此代碼資源res = this.getResources();按鈕2。setBackgroundDrawable(this.Sd(res.getColor(R.color.color1x2))); – max4ever 2011-05-17 13:08:33

相關問題