java – 没有列表项分隔符的ListView头
发布时间:2020-12-15 03:00:35 所属栏目:Java 来源:网络整理
导读:我目前正在编写一个使用带有标题的ListView的 Android应用程序.它工作正常,但不是我想要的. ListView中的每个项目在其顶部和底部都有1-2px分隔符.标题也是如此 – 这就是问题所在.它看起来不太漂亮…… 有趣的是,系统应用程序(例如“设置”)没有这样的问题.
我目前正在编写一个使用带有标题的ListView的
Android应用程序.它工作正常,但不是我想要的. ListView中的每个项目在其顶部和底部都有1-2px分隔符.标题也是如此 – 这就是问题所在.它看起来不太漂亮……
有趣的是,系统应用程序(例如“设置”)没有这样的问题. 这是我的示例适配器: setListAdapter(new BaseAdapter() { @Override public int getCount() { return 10; } @Override public Object getItem(int i) { return i; } @Override public long getItemId(int i) { return i; } @Override public View getView(int i,View view,ViewGroup viewGroup) { View v = ((LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)) .inflate(i % 3 == 0 ? R.layout.list_header : android.R.layout.simple_list_item_1,viewGroup,false); ((TextView)v.findViewById(android.R.id.text1)).setText("test"); return v; } }); 并列出标题布局文件: <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello,World" style="?android:attr/listSeparatorTextViewStyle"> </TextView> 所以问题是:如何摆脱标题和常规项之间的项分隔符,就像,例如,设置应用程序吗? 编辑: 解决方法
您似乎必须使用自定义项目视图进行分隔和一些解决方法.让我解释一下如何管理这个:
>不要使用默认分频器,remove it. 然后,两种类型的分隔器将是胶合,只为头部分制作一个子线,因此分隔器应具有相同的高度以便形成良好的子线.这将避免在标题部分上方具有分隔符,但保留项目列表的分隔符. 因此,让我展示一些代码来实现它.首先,不要忘记避免ListView上的默认分隔符: <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="@null" android:dividerHeight="0dp"/> 创建一个项目布局,顶部的分隔符设置为1dp(或其他): <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- this is the divider for items --> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/darker_gray"/> <!-- and the rest of item's content in another ViewGroup --> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" android:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher"/> <TextView android:id="@+id/item_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="15dp" android:textColor="@android:color/white"/> </LinearLayout> </LinearLayout> 最后,标题布局在底部有一个分隔符(与项目的分隔符高度相同): <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/item_header" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" style="?android:attr/listSeparatorTextViewStyle"/> <!-- this is the ("half-")divider for header section --> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/darker_gray"/> <!-- this view above will be merged with item's dividers --> </LinearLayout> 它给出了这个结果: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |