2010-12-14 11 views
7

AndroidでカスタムViewを作成します。私はできるだけシンプルにしようとしており、ほとんど空のクラスMyViewを作成し、それを私のLinearLayoutで使用しましたが、アプリケーションは「強制終了」で開始すると失敗します。どのように私は簡単なカスタムViewを行うことができますか? Building Custom Componentsによると、Viewは、onMeasure()を上書きしなければ100x100のサイズになります。シンプルなカスタムビューを作成するには?

public class MyView extends View { 

    public MyView(Context context) { 
     super(context); 
    } 
} 

そして私はとLinearLayoutにそれを使用する:私は間違って何をやっている

<view 
    class="com.example.MyView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.0" /> 


私が提案しitemonコンストラクタと、スーパークラスに対応するコールを使用している場合。その後、「強制終了」はなくなりましたが、LinearLayoutが壊れていて、MyView以降のコンポーネントは表示されません。ここで

は私 main.xmlです:

public MyView(Context context, AttributeSet attrs) 

は、Androidフレームワークは、上記のコンストラクタからのビューでUIを構築しようとします:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    > 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.0" 
    android:background="#f00" 
    android:text="Hello" 
/> 
<view 
    class="com.example.MyView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.0" 
/> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.0" 
    android:background="#00f" 
    android:text="World" 
/> 
</LinearLayout> 
+0

ここで良いサンプルを調べることができます:[http://www.sgoliver.net/blog/?p=1457](http://www.sgoliver.net/blog/?p=1457) –

+0

私は似ていますあなたは必要なものを手に入れましたか? .share some code plz – Nepster

答えて

9

は、あなたがこのような別のコンストラクタメソッドを定義することができかもしれ。

+0

ありがとう、それは "強制終了"メッセージを削除しますが、私の 'LinearLayout'は壊れて、後のコンポーネントは表示されません。 – Jonas

+2

私は 'onMeasure()'もオーバーライドしなければなりませんでした。今それは素晴らしい作品です。 – Jonas

9

Androidデベロッパーガイドにカスタムコンポーネントの作成というセクションがあります。残念なことに、XML属性の議論では、レイアウトファイル内のコントロールを宣言し、実際にはクラスの初期化の中の値を処理しないだけです。手順は次のとおりです。

宣言は、値の属性\ attrs.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="MyCustomView"> 
     <attr name="android:text"/> 
     <attr name="android:textColor"/>    
     <attr name="extraInformation" format="string" /> 
    </declare-styleable> 
</resources> 

お知らせ宣言-styleableタグ内の非修飾名を使用します。 extraInformationのような非標準のアンドロイド属性は、その型を宣言する必要があります。スーパークラスで宣言されたタグは、再宣言することなくサブクラスで使用できます。初期化のための属性セットを使用する2つのコンストラクタがあるので

はコンストラクタ

を作成し、コンストラクタを呼び出すための個別の初期化メソッドを作成すると便利です。

private void init(AttributeSet attrs){ 
    TypedArray a=getContext().obtainStyledAttributes(attrs,R.styleable.MyCustomView); 
    //Use a 
    Log.i("test",a.getString(R.styleable.MyCustomView_android_text)); 
    Log.i("test",""+a.getColor(R.styleable.MyCustomView_android_textColor, Color.BLACK)); 
    Log.i("test",a.getString(R.styleable.MyCustomView_android_extraInformation)); 
    //Don't forget this 
    a.recycle(); 
} 

R.styleable.MyCustomViewは、自動生成されたint []リソースで、各要素は属性のIDです。要素名に属性名を追加することにより、XMLの各プロパティに対して属性が生成されます。さまざまなget関数を使用して、TypedArrayから属性を取得できます。属性がXMLで定義されていない場合、nullが戻されます。もちろん、戻り値の型がプリミティブである場合を除き、2番目の引数が返されます。

すべての属性を取得したくない場合は、この配列を手動で作成することができます。標準のAndroid属性のIDはandroid.R.attrに含まれ、このプロジェクトの属性はRです。 attr。

int attrsWanted[]=new int[]{android.R.attr.text, R.attr.textColor}; 

それが将来的に変更される可能性があり、このスレッドごととしてあなたは、android.R.styleableで何を使うべきではないことに注意してください。これらの定数をすべて1つの場所で表示することは便利です。

のxmlnsを名前空間宣言を含め、このようなレイアウトの\ main.xmlとしてレイアウトファイルで使用してください:アプリ= "http://schemas.android.com/apk/res/com.mycompany .projectname "

最上位のxml要素です。

<com.mycompany.projectname.MyCustomView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@android:color/transparent" 
    android:text="Test text" 
    android:textColor="#FFFFFF" 
app:extraInformation="My extra information"; 
/> 

完全修飾名を使用してカスタムビューを参照します。

アンドロイドLabelViewなどのサンプル

あなたは完全な例をしたい場合は、Androidのラベル表示サンプルを見てください。

LabelView.java

TypedArray a=context.obtainStyledAttributes(attrs, R.styleable.LabelView); 
CharSequences=a.getString(R.styleable.LabelView_text); 
attrs.xml 

<declare-styleable name="LabelView"> 
    <attr name="text"format="string"/> 
    <attr name="textColor"format="color"/> 
    <attr name="textSize"format="dimension"/> 
</declare-styleable> 

custom_view_1.xml

<com.example.android.apis.view.LabelView 
    android:background="@drawable/blue" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    app:text="Blue"app:textSize="20dp"/> 

これは、名前空間属性を持つのLinearLayoutに含まれています。

のxmlns:アプリ= "のhttp:// schemas.android.com/apk/res/com.example.android.apis "

関連する問題