2012-08-24 10 views
7

私はこれを行う方法を知っていたかのように感じますが、私は現在空白を描いています。私はView(Card)から拡張されたクラスを持っており、XMLのレイアウトを書いています。私がしたいのは、CardのビューをコンストラクタのXMLビューに設定することです。そのため、Cardのメソッドを使用してTextViewとそれ以外を設定することができます。助言がありますか?以下のコード:ビューサブクラスのビューとしてXMLレイアウトを使用しますか?

Card.java:(私は何をしたいのかの例としてそこにありますが、うまくいきません。私は基本的に、そのクラスをViewのインターフェースにしたいと思います。私は何とかビューにXMLレイアウトを割り当てる必要があります。私はsetContentView(View view)の線に沿って何かを使用していますか?Viewクラスには、このような方法はありませんが、それのようなものがあるのでしょうか?それ)

public class Card extends View { 

    TextView tv; 

    public Card(Context context) { 
     super(context); 
     View.inflate(context, R.layout.card_layout, null); 
     tv = (TextView) findViewById(R.id.tv); 
    } 

    public Card(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     View.inflate(context, R.layout.card_layout, null); 
     tv = (TextView) findViewById(R.id.tv); 
    } 

    public Card(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     View.inflate(context, R.layout.card_layout, null); 
     tv = (TextView) findViewById(R.id.tv); 
    } 

    public void setText(String text) { 
     tv.setText(text); 
    } 

} 

card_layout。 xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="336dp" 
    android:layout_height="280dp" 
    android:layout_gravity="center" 
    android:background="@drawable/card_bg" 
    android:orientation="vertical" > 


    <TextView 
     android:id="@+id/tv" 
     android:layout_height="fill_parent" 
     android:layout_width="wrap_content" 
     android:textSize="24dp" 
    /> 

</LinearLayout> 

答えて

10

現在の設定では不可能です。 View(またはそれの直接のサブクラス)は、の単一のというビューを表しています。子ビューという概念はありません。何をしようとしていますか。 クラスには実際に子を追加するメソッド(addView()メソッドのような)がないため、LayoutInflaterは単純なViewで使用することはできません。

一方、子供を持つことができるように使用する正しいクラスがaddViewを供給することにより、ViewGroup(又はLinearLayoutFrameLayout等のような直接のサブクラスの1つ)、それにViewsまたは他のViewGroupsを添加受け入れるありますメソッド。最後に、あなたのクラスは次のようになります。

public class Card extends ViewGroup { 

    TextView tv; 

    public Card(Context context) { 
     super(context); 
     View.inflate(context, R.layout.card_layout, this); 
     tv = (TextView) findViewById(R.id.tv); 
    } 

    public Card(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     View.inflate(context, R.layout.card_layout, this); 
     tv = (TextView) findViewById(R.id.tv); 
    } 

    public Card(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     View.inflate(context, R.layout.card_layout, this); 
     tv = (TextView) findViewById(R.id.tv); 
    } 

    public void setText(String text) { 
     tv.setText(text); 
    } 

} 

私はあなたがViewGroupを拡張する場合onLayoutをオーバーライドする必要があり、その代わりに(と理由あなたのレイアウトファイルの)リコール場合は、LinearLayoutの延長を見てからLinearLayoutを交換する必要がありますmergeタグを持つxmlレイアウト。

+0

私のクラスが 'LinearLayout'を拡張していると思って、それがダムのアイデアだと思った。私はこのコメントを見て、それを試して、それは完全に働いた!どうもありがとうございました! – roboguy12

3

レイアウトでクラス名を使用できるはずです。次のようなものがあります。

<your.package.name.Card 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="336dp" 
    android:layout_height="280dp" 
    ... 

子ビューを取得するには、ちょうどfindViewByIdを呼び出します。

Cardクラスを使用するには、インフレータを使用してインスタンスを取得し、それを親ビューに添付します。あなたが何をしようとして

LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 
Card card = (Card) inflater.inflate(R.layout.card_layout, null); 
parentView.addView(card); 

Have a look at the docs for more info.

+0

しかし、 'card_layout.xml'は' Card'クラスにどのように接続されていますか?私があなたが上に書いたことをすると、何も現れません。 – roboguy12

+0

私はドキュメントを見て、それはプログラムでカスタムビューを描画するのにXMLで定義されたものを使用することについては何も情報を提供しません。私は方法などを使ってすべてを描くことができましたが、それはかなり詳細なレイアウトなので、しばらく時間がかかります。 – roboguy12

+0

'card'のインスタンスを' view.addView(card) 'または' setContentView(card) 'を使って別のビューに追加する必要があります。 – devnate

関連する問題