是否有一个XML标记,等同于'ListView.addHeaderView“? [英] Is there an XML tag that is equivalent to `ListView.addHeaderView'?

查看:122
本文介绍了是否有一个XML标记,等同于'ListView.addHeaderView“?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个XML标签,我可以在一个布局文件,等效于<一个使用href="http://developer.android.com/reference/android/widget/ListView.html#addHeaderView%28android.view.View%29"><$c$c>ListView.addHeaderView()?

Is there an XML tag which I can use in a layout file that is equivalent to ListView.addHeaderView()?

推荐答案

我写了一个简单的的ListView 喜欢你的要求。

I wrote a simple ListView like your requirement.

  1. 值声明自定义属性在 attrs.xml 文件夹:

  1. Declare custom attribute in attrs.xml in value folder:

<resources>
    <declare-styleable name="HeaderListViewFromXML"> 
        <attr name="headerView" format="reference"/>
    </declare-styleable>
</resources>

  • 创建 HeaderListViewFromXML 类扩展的ListView

  • Create HeaderListViewFromXML class extended ListView

    public class HeaderListViewFromXML extends ListView {
        private int headerId;
    
        public HeaderListViewFromXML(Context context) {
            this(context, null);
        }
    
        public HeaderListViewFromXML(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }
    
        public HeaderListViewFromXML(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.HeaderListViewFromXML, defStyle, defStyle);
    
            try {
                headerId = a.getResourceId(R.styleable.HeaderListViewFromXML_headerView, View.NO_ID);
                if (headerId != View.NO_ID) {
                    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    View header = inflater.inflate(headerId, null);
                    addHeaderView(header);
                }
            } finally {
                a.recycle();
            }
        }
    }
    

  • 在声明 layout.xml

    <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        <com.example.rewidget.HeaderListViewFromXML
                android:id="@+id/listWithHeader"
                android:layout_width="fill_parent"
                android:layout_height="150dp"
                android:layout_marginTop="60dp"
                android:background="#00FF00"
                // custom attribute. Point to layout in header1.xml
                app:headerView="@layout/header1" />
    </RelativeLayout>
    

  • 在活动中,使用像正常的ListView

    public class MainActivity extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ListView list = (ListView) findViewById(R.id.listWithHeader);
    
            String[] values = new String[] { "Android", "iPhone", "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2" };
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values);
    
            list.setAdapter(adapter);
        }
    }
    

  • 这篇关于是否有一个XML标记,等同于'ListView.addHeaderView“?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆