gpt4 book ai didi

android - 如何将 listView 标题添加为自定义布局

转载 作者:太空宇宙 更新时间:2023-11-03 12:16:05 24 4
gpt4 key购买 nike

我想在 listView 中添加 header 我尝试这样我在这里缺少的 id 很好。我在 fragment 中使用它。

ListView lv;

LinearLayout header = (LinearLayout) rootView.findViewById(R.id.header_layout);

lv.addHeaderView(header);
// APP is crashing here?

逻辑猫

java.lang.ClassCastException: android.widget.AbsListView$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
at android.widget.RelativeLayout$DependencyGraph.findRoots(RelativeLayout.java:1732)
at android.widget.RelativeLayout$DependencyGraph.getSortedViews(RelativeLayout.java:1677)
at android.widget.RelativeLayout.sortChildren(RelativeLayout.java:381)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:389)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.view.View.measure(View.java:17547)

最佳答案

我们可以像下面这样给 ListView 添加 header :

LayoutInflater myinflater = getLayoutInflater();
ViewGroup myHeader = (ViewGroup)myinflater.inflate(R.layout.headerlayout, myListView, false);
myListView.addHeaderView(myHeader, null, false);

但是,根据您的错误,您可能在 xml 和 java 文件中采用了相对布局,您正在使用 LinearLayout。

您还必须将 ListView 初始化为:

lv=(ListView)findViewById(R.id.yourlistview);`

关于android - 如何将 listView 标题添加为自定义布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35361775/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com