1

子の下に透明な角丸四角形を描画するカスタム レイアウトがあります。問題は、xml ファイルに追加しようとすると表示されないことです。また、パラメーターを追加しようとすると (つまりandroid:layout_width)、ポップアップに、使用できるパラメーターがないことが示されます。追加した子ビューにも同じことが起こります。誰でも助けることができますか?

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

3 に答える 3

8

LinearLayout の角丸長方形の背景が必要な場合は、「res/drawable/」の xml ファイルで描画可能な形状を定義できます。

<?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 桁は色を表します。

于 2010-07-27T08:52:02.770 に答える
1

このチュートは本当にあなたの問題を解決してくれました

まず、カスタム レイアウトに渡したいフィールドをファイル res/values/attrs.xml で定義する必要があります。

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

これらの属性を受け取るようにコンストラクターを準備します

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

次に、名前空間を layout.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>

ただし、長方形を描きたいだけの場合は、他の回答で提案されている方法を使用できます

于 2011-11-25T12:10:56.757 に答える
0

失敗するのはプラグインだと思いますが、android:layout_* を指定すると考慮されます。とにかく、丸い長方形のレイアウトを描きたいだけなら、マルコの指示に従ってください

于 2010-08-10T18:49:25.970 に答える