2010-12-15 2 views
2

私はAndroid開発を初めて使いました。 TextView、EditText、およびSkillDiceButton(Buttonクラスの拡張機能)のコンポジットビュー(SkillDiceGroup)を作成したいとします。私はそれが私のSkillDiceGroupを宣言するとき、純粋なコードとして働いて、そして私のXMLレイアウトでこれを入れてありますAndroid XMLコンポジットビュー

<com.jeremybush.d20.SkillDiceGroup android:id="@+id/skillDiceTest" 
    android:title="Foobar!" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    </com.jeremybush.d20.SkillDiceGroup> 

そして、私はこのコードを持っている:

public class SkillDiceGroup extends LinearLayout 
{ 
// The View components 
private TextView mTitle; 
private EditText mSkill; 
private SkillDiceButton mDice; 

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

    this.setOrientation(HORIZONTAL); 

    mTitle = new TextView(context); 
     mTitle.setText("foobar"); 
    addView(mTitle, new LinearLayout.LayoutParams(
    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT 
)); 

    mSkill = new EditText(context); 
    addView(mSkill, new LinearLayout.LayoutParams(
    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT 
)); 

    mDice = new SkillDiceButton(context, attrs); 
    mDice.setText("d20"); 
    addView(mDice, new LinearLayout.LayoutParams(
    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT 
)); 
} 

private class SkillDiceButton extends DiceButton 
{ 
    public SkillDiceButton(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    } 

    public void onClick(View view) 
    { 
    modifier = Integer.parseInt(mSkill.getText().toString()); 

    super.onClick(view); 
    } 
} 
} 

これは私がそれをしたいどのように動作しますが、私は希望3つの項目をxmlビューで独自に宣言するのが好きです。これどうやってするの?

答えて

1

declaring layoutcommon layout objectshello viewsのような、xmlレイアウトに関するAndroidの豊富なドキュメントをご覧ください。各レイアウトタイプの詳細な例があります。ただのEditTextとSkillDiceButtonと第二の二のテキストビューを置き換える

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1"> 
    <TextView 
     android:text="row one" 
     android:textSize="15pt" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 
    <TextView 
     android:text="row two" 
     android:textSize="15pt" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 
    <TextView 
     android:text="row three" 
     android:textSize="15pt" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 
    <TextView 
     android:text="row four" 
     android:textSize="15pt" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 
    </LinearLayout> 

:のLinearLayoutチュートリアル、3つの項目とレイアウトから直接取得

+0

私はドキュメントを読んでいますが、この例は全く同じではありません。私のコードの最後には、mSkill変数を参照する修飾子の割り当てがあります。私が必要とする論理のために、あなたの例は直接の翻訳ではないと思います。私は間違っているかもしれませんが(そしておそらく午前)。 – zombor

+0

ロジックは、ビュークラスではなくアクティビティクラスに配置する必要があります。レイアウトの要素にアクセスするには、idを使用してテキストを表示するか、クリックリスナーを追加する必要があります。メモ帳のチュートリアル(http://developer.android.com/guide/tutorials/notepad/index.html)のような完全な例の1つを使って、すべてがどのように適合すると思われるかを調べるのに役立ちます。 –

+0

これは正しい方法です。カスタムビューに独自の属性が必要な場合は、独自のxmlnsも追加できます。このプロジェクトのパネルウィジェットには、独自の属性を追加する方法の例があります。 http://code.google.com/p/android-misc-widgets/ findViewByIdを使用して、アクティビティ内のカスタムビューへの参照を取得し、余分な属性を手動で設定することもできます。どちらの解決策も間違っていません。 – schwiz

0

あなたは「非表示」内部レイアウト構造という独自のビュー/ウィジェットを作成するためにwan't、および様々な場所で再利用可能な場合 - あなたはおよそcreating custom components

もタグについて読み、LayoutInflater

文書をお読みください

しかし、SkillDiceGroupを一度しか使用しない場合は、Mayraの提案通りにレイアウトを作成してください

+0

他のコメントで言ったように、私はこのことを15-20回再利用します。 – zombor

関連する問題