簡體   English   中英

如何從布局覆蓋視圖的方法?

[英]How do I override methods of a view from Layout?

我想知道如何從代碼中擴展布局內部的View類。 我看到了不同的示例,例如:

listView = new ListView(this){
   //Override some methods here
 }

現在,如果我已經在布局中擁有ListView並且仍然要覆蓋某些內容,該怎么辦。 我可以從布局內部引用ListView的唯一方法是:

listView = (ListView)findViewById(R.id.mylistView);

因此,從此示例如何覆蓋列表視圖的方法。 請我只是一個普通的android程序員。 請賜教。

您要么必須以編程方式構建布局(這通常很乏味且煩人),要么必須在命名類中將ListView子類化,然后將其添加到布局中。 這是第二個選項的樣子:

public class MyListView extends ListView {

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

    public MyListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    // override other methods here
}

在您的布局文件中,使用

<com.your.package.MyListView
    android:layout_width="..."
    android:layout_width="..."
    ... />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM