本文介绍了由ListFragment的适配器显示列表前添加自定义布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个 ListFragment 显示列表。都好。我希望把一些格式良好的TextView preceding名单。结果我试图与一个TextView创建线性布局和增加它作为一个报头添加到列表中,但它不工作。结果以下不工作:I have a ListFragment that displays a list. All good. I wanted to put some well formatted textview preceding the list.I tried creating a linear layout with a textview and adding it as a header to the list but it does not work.The following does work:TextView tv = new TextView(getActivity()); tv.setText("Some title with comments"); getListView().addHeaderView(tv); tv.setGravity(Gravity.LEFT); tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);我得到的列表前的文本,但我想实际设置的布局,而不是因为我想有2种不同的字体(它就像一个标题/字幕大字体和小说明较小的字体)。文字我该怎么办呢?结果我没有与定义列表视图的布局。我只有一个 ListFragment 和我的自定义listAdapterI get the text before the list but I would like to actually set a layout instead as I would like text with 2 different fonts (it is like a title/caption with big font and a small description with smaller font).How can I do that?I don’t have any layout with list view defined. I only have a ListFragment and my custom listAdapter 更新:结果我也试过,没有工作:Update:What I also tried and did not work: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { this.headerView = inflater.inflate(R.layout.my_list_header, container, false); return super.onCreateView(inflater, container, savedInstanceState); }和后来设置适配器时:结果 getListView()addHeaderView(this.headerView); 结果但我得到的例外And later when setting the adapter:getListView().addHeaderView(this.headerView);But I got exceptions推荐答案下面是步骤在ListFragment适当添加标题:Here are the steps for properly adding a header in a ListFragment:@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View list_root = inflater.inflate(R.layout.fragment_list, null); // Get the list header - to be added later in the lifecycle // during onActivityCreated() mheaderView = inflater.inflate(R.layout.my_list_header, null); return list_root; }@Overridepublic void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if (mheaderView != null) this.getListView().addHeaderView(headerView); // Don't forget to now call setListAdapter() this.setListAdapter(listAdapter);}@Overridepublic void onDestroyView(){ super.onDestroyView(); // free adapter setListAdapter(null);} 这篇关于由ListFragment的适配器显示列表前添加自定义布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-03 01:56