2012-08-24 49 views
7

我感覺好像我曾經知道如何做到這一點,但我目前正在畫空白。我有一個從View(Card)延伸出來的類,我用XML編寫了它的佈局。我想要做的是將Card的視圖設置爲構造函數中的XML視圖,因此我可以使用Card中的方法設置TextView s以及什麼。有什麼建議麼?下面的代碼:使用XML佈局作爲View子類的視圖?

Card.java: (我有View.inflate(context, R.layout.card_layout, null);那裏的是我想要做的例子,但它不工作我基本上希望類是視圖的接口,爲了做我需要以某種方式分配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

什麼你試圖做是不可能的當前設置。 A View(或其直接子類)代表單一視圖它不具有子視圖的概念,您嘗試執行的操作。 LayoutInflater不能與簡單的View一起使用,因爲簡單的View類沒有實際添加子項的方法(如addView()方法)。

在另一方面,正確的類使用才能夠生孩子是ViewGroup(或直接子類像LinearLayout之一,FrameLayout等)和接受加入Views或其他ViewGroups它,通過提供addView方法。到底你的類應該是:

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

} 

如果我還記得你要重寫onLayout如果擴展ViewGroup,所以不是(因爲你的佈局文件的),你應該看看擴展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讓孩子的意見。

要使用您的卡類,請使用充氣器獲取實例,然後將其附加到父視圖。

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

相關問題