2010-07-26 35 views
1

我有一個自定義佈局,它的子級下繪製一個透明的圓角矩形。問題是當我嘗試將它添加到我的xml文件時,它不顯示。另外,當我嘗試向其添加參數時(即android:layout_width),彈出窗口顯示它們都不可用。同樣的事情發生在我添加的任何子視圖上。誰能幫忙?編寫自定義LinearLayout爲Android

public class RoundRectLayout extends LinearLayout 
{ 
private RectF shape; 
public RoundRectLayout(Context context) 
{ 
    super(context); 
    LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    layoutInflater.inflate(R.layout.settings, this); 
    shape = new RectF(); 
} 

public RoundRectLayout(Context context, AttributeSet attrs) 
{ 
    super(context, attrs); 
    LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    layoutInflater.inflate(R.layout.settings, this); 
    shape = new RectF(); 
} 

@Override 
protected void onSizeChanged(int w, int h, int oldw, int oldh) 
{ 
    shape = new RectF(0, 0, w - 5, h - 5); 
    super.onSizeChanged(w, h, oldw, oldh); 
} 

@Override 
protected void dispatchDraw(Canvas canvas) 
{ 
    Paint temp = new Paint(); 
    temp.setAlpha(125); 
    canvas.drawRoundRect(shape, 10f, 10f, temp); 
    super.dispatchDraw(canvas); 
} 
} 

回答

8

如果你只是想爲你的LinearLayout一個圓角矩形背景,你可以在XML文件中的「RES /繪製/」定義了在繪製形狀:

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="#50000000"/>  
    <stroke android:width="3dp" 
      android:color="#ffffffff"/> 
    <padding android:left="5dp" 
      android:top="5dp" 
      android:right="5dp" 
      android:bottom="5dp"/> 
    <corners android:bottomRightRadius="7dp" 
      android:bottomLeftRadius="7dp" 
      android:topLeftRadius="7dp" 
      android:topRightRadius="7dp"/> 
</shape> 

然後你設置佈局的背景到你的可繪製XML文件的名稱。如果你把它命名爲round_border.xml,將背景設置爲:

<LinearLayout 
    android:background="@drawable/round_border" 
    ... 

的透明度設置在下面......

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

2位數字表示的透明度,最後6位是顏色。

0

我認爲這是插件失敗,但你可以指定android:layout_ *,它會被考慮在內。無論如何,如果你只是想畫一個圓矩形佈局,跟蹤Marco的指示

1

該政黨成員真的幫了我與yourp roblem

首先你必須定義你想傳遞給您的自定義佈局的領域文件RES /價值/ attrs.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="MyLayout"> 
     <attr name="text" format="string" /> 
    </declare-styleable> 
</resources> 

現在準備構造函數來recive這些屬性

public RoundRectLayout(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    TypedArray a = context.obtainStyledAttributes(attrs, 
      R.styleable.MyLayout); 
    CharSequence s = a.getString(R.styleable.RoundRectLayout_text); 
    if (s != null) { 
     mText = s.toString(); 
    } 
    a.recycle(); 
} 

一個命名空間然後添加到您的拉yout.xml,以便android konws可以查找您的自定義佈局。 「mi.pakage」是您的自定義佈局的Java包。

一旦你有你的命名空間和類只是叫你attrs.xml

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:myapp="http://schemas.android.com/apk/res/mi.pakage" 
    android:id="@+id/root" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#fff"> 

    <com.example.MyLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     myapp:text="My Special Text String"/> 

</RelativeLayout> 

設置atributes但如果你只是想繪製一個矩形,您可以使用該方法在對方的回答sugested