如何将headerview放入gridview中

发布于 2024-12-05 07:24:41 字数 77 浏览 2 评论 0原文

我必须将标题视图放在网格视图的顶部,例如列表视图。尽管我知道它不支持这一点。但有没有什么办法可以拥有呢。只是想知道是否有人做过这方面的工作?

I have to put header view on top of grid view like list view. Although I know it doesn't have support for that. But is there any way to have it. Just wanted to know if anyone has done work on it ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏日浅笑〃 2024-12-12 07:24:41

你总是有一个解决方法,比如为页眉和页脚使用 textview,

在 listview 的情况下将 gridview 放在中间,答案将是这样的:

你当然可以在 ListView 周围放置任意元素。例如,您可以定义一个包含两个 TextView 和它们之间的 ListView 的布局。如果这样做,则必须将 id“@android:id/list”分配给 ListView,因为 ListActivity 会搜索具有此 id 的视图。如果执行此操作,则一个 TextView 将始终在 List(标题)上方可见,而另一个 TextView 将在 ListView 下方可见。如果您只想在看到列表的开头/结尾时显示页眉/页脚视图,您可以使用 view.setHeaderView() 或 view.setFooterView()。例如:

public class MyList extends ListActivity {

    /** Called when the activity is first created. */
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        // Create an array of Strings, that will be put to our ListActivity
        String[] names = new String[] { "Linux", "Windows7", "Eclipse", "Suse",
                "Ubuntu", "Solaris", "Android", "iPhone", "Linux", "Windows7",
                "Eclipse", "Suse", "Ubuntu", "Solaris", "Android", "iPhone" };
        View header = getLayoutInflater().inflate(R.layout.header, null);
        View footer = getLayoutInflater().inflate(R.layout.footer, null);
        ListView listView = getListView();
        listView.addHeaderView(header);
        listView.addFooterView(footer);
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_single_choice,
                android.R.id.text1, names));

    }
}

对于 gridview 的情况,您需要做自己的布局
- 顶部的文本视图
-gridview
-textview 在底部,它的概念是相同的,

你只是不必说 listView.addHeaderView(header);
因为你会通过布局上的设计来实现这种效果

You always have a workaround like having textview for header and footer with gridview in between

in case of listview the answer will be like this :

You can of course put arbitray elements around your ListView. For example you can define a layout with two TextViews and a ListView between them. If you do this, you must assign the id "@android:id/list" to the ListView, as the ListActivity searches for a view with this id. If you do this then one TextView will always be visible above the List (header) and the other will be visible below the ListView. If you want to display the header / footer view only if see the beginning / end of the list you can use view.setHeaderView() or view.setFooterView(). For example:

public class MyList extends ListActivity {

    /** Called when the activity is first created. */
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        // Create an array of Strings, that will be put to our ListActivity
        String[] names = new String[] { "Linux", "Windows7", "Eclipse", "Suse",
                "Ubuntu", "Solaris", "Android", "iPhone", "Linux", "Windows7",
                "Eclipse", "Suse", "Ubuntu", "Solaris", "Android", "iPhone" };
        View header = getLayoutInflater().inflate(R.layout.header, null);
        View footer = getLayoutInflater().inflate(R.layout.footer, null);
        ListView listView = getListView();
        listView.addHeaderView(header);
        listView.addFooterView(footer);
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_single_choice,
                android.R.id.text1, names));

    }
}

for case of gridview you need to do your own layout that will have
-textview on the top
-gridview
-textview on the bottom and that it the concept is the same

you just do not have to say listView.addHeaderView(header);
cause you will make that effect with the design on the layout

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文