0

私はLinearLayoutのサブクラスであるカスタムコントロールを作成しました。私はまた、このコントロールが基づいているレイアウトファイルを作成しました。最後に、カスタムプロパティを設定するためにコンストラクタで解析する属性を定義しました。たとえば、これらのプロパティの1つを「テキスト」と呼びます。レイアウトに基づいてXMLに優しいコントロールを作成するにはどうすればよいですか?

ここに私のコードの簡易版(私たちはただ一つの特性「テキスト」に集中できるように、私は他のプロパティの多くを剥奪し、このようました)です:

まず、クラス(私たちのカスタムラジオボタン)のバージョン...ここ

public class RadioButton extends LinearLayout 
{ 
    private TextView textView; 

    public RadioButton(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 
     initAttributes(attrs, 0); 
    } 

    private void initAttributes(AttributeSet attrs, int defStyle) 
    { 
     final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CheckBoxView, defStyle, 0); 

     text = a.getString(R.styleable.RadioButton_text); 
     if(text == null) 
      text = "Not set"; 

     a.recycle(); 
    } 

    @Override 
    protected void onFinishInflate() 
    { 
     super.onFinishInflate(); 

     textView = (TextView)findViewById(R.id.textView); 
     textView.setText(text); 
    } 

    private String text; 
    public String getText() { return text; } 
    public void setText(String newValue) 
    { 
     text = newValue; 

     if(textView != null) 
      textView.setText(text); 
    } 
} 

がここに...

<resources> 

    <attr name="text" format="string" /> 

    <declare-styleable name="RadioButton"> 
     <attr name="text" /> 
    </declare-styleable> 

</resources> 

attrs.xmlファイルだとは 'reusable_radiobutton.xml' レイアウトファイル(注です:内部RadioButtonViewがありますカスタムレンダリングビューと正常に動作します):上記で

<?xml version="1.0" encoding="utf-8"?> 
<com.somedomain.reusable.ui.RadioButton 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical"> 

    <com.somedomain.reusable.ui.RadioButtonView 
     android:id="@+id/radioButtonView" 
     style="@style/DefaultRadioButtonView" /> 

    <TextView 
     android:id="@+id/textView" 
     style="@style/DefaultRadioButtonText" /> 

</com.somedomain.reusable.ui.RadioButton> 

、私のコントロールのユーザーは単にのようなので、自分のレイアウトファイルに含めることができます...

<include android:id="@+id/someRadioButton" 
    layout="@layout/reusable_radiobutton" /> 

ザ・自分のコードで次のようにして、彼らはそのインスタンスを取得し、望むようにすることができます。

RadioButton someRadioButton = (RadioButton)findViewById(R.id.someRadioButton); 
someRadioButton.text = "Woot!"; 

これは期待どおりに動作します。

しかし、これは...

<include android:id="@+id/someRadioButton" 
    layout="@layout/reusable_radiobutton" 
    app:text="Hello World!" /> 

それは私に警告を与えませんが、そうでない場合は、それを無視します。

私はこれを試してみました...

<com.somedomain.reusable.ui.RadioButton 
    app:text="Hello World!" /> 

これは私のコントロールをインスタンス行い、合格していますが 'Hello Worldのを!'属性を介して私のプロパティに、何も実際にロードするか、レイアウトファイルをクラスに関連付けることがないので、何も画面に表示されません!

レイアウトに基づいてカスタムビューを作成するにはどうすればいいですか?他の開発者は独自のレイアウトファイルで参照するだけで、カスタム属性を設定することもできます。

すべてが意味を成し遂げました。 :)

注:私は後だ、まさにについてのAndroidドキュメント会談、「化合物コントロールズas referenced hereが、彼らは配合コントロールを定義するには、レイアウトの使用例を与えることはありません。しかし、私はそれがかなり近いと感じています。

答えて

0

それはhereが見つかりました。私はあなたが自分自身に何かを膨らませることができるとは思わなかった。それとMergeタグを使用して私のためにそれを世話しました。

関連する問題