【问题标题】:Is there an XML tag that is equivalent to `ListView.addHeaderView'?是否有等同于“ListView.addHeaderView”的 XML 标记?
【发布时间】:2014-11-13 23:44:29
【问题描述】:

是否有可以在布局文件中使用的 XML 标记,相当于 ListView.addHeaderView()

【问题讨论】:

  • 我认为它在原始列表视图中不存在。一种可能的解决方案是扩展ListView 并创建自定义属性来设置标题视图。
  • @Yul 我可以创建可在 XML 布局文件中使用的自定义属性吗?我将不得不进一步调查......
  • Here好友
  • @Yul Cool,我去看看。

标签: java android android-layout


【解决方案1】:

我按照您的要求写了一个简单的ListView

  1. value文件夹中的attrs.xml中声明自定义属性:

    <resources>
        <declare-styleable name="HeaderListViewFromXML"> 
            <attr name="headerView" format="reference"/>
        </declare-styleable>
    </resources>
    
  2. 创建HeaderListViewFromXML类扩展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();
            }
        }
    }
    
  3. layout.xml 中声明自定义HeaderListViewFromXML

    <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>
    
  4. 在Activity中,像平常一样使用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);
        }
    }
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-10
    • 2021-09-04
    • 1970-01-01
    • 1970-01-01
    • 2023-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多