【问题标题】:ListView dividers on top and bottom of the ListView ItemListView 项目顶部和底部的 ListView 分隔线
【发布时间】:2013-04-23 10:53:43
【问题描述】:

我正在使用 Android Listview,并且我希望在 listview 项目的两侧都有一个 1px 的分隔线,这意味着在两种不同颜色的顶部和底部。但问题是我没有登录以在底部显示分隔线。我试过android:layout_below,但显示无效。

这是列表视图代码

<ListView
        android:id="@+id/myphnview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@drawable/dividerheight"
        android:background="#E9EAEC"
        android:clickable="true"
        android:divider="@drawable/dividerheight" >
    </ListView>

这是我用于顶部边框的 xml 文件。分割高度.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape android:shape="line" >
            <stroke android:color="#c6c7c9" />

            <size android:height="1px" />
        </shape>
    </item>


</layer-list>

这是我的行的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rowlayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#E9EAEC"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2" >

        <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp" 
            android:background="@drawable/ic_launcher"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_weight="1" >

        <TextView
            android:id="@+id/file_name"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_margin="5dp"
            android:text="Hello Android "
            android:textColor="@android:color/black"
            android:textSize="20dp" >
        </TextView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp" >

        <ImageView
            android:id="@+id/share_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:background="@drawable/ic_launcher"/>
    </LinearLayout>



</LinearLayout>

【问题讨论】:

  • 您是否为列表视图使用自定义适配器?
  • @Devu Soman 是的,它是一个自定义适配器
  • 我想你想在列表项的顶部和底部设置两种不同的颜色。然后尝试从列表适配器的 getView() 设置分隔符,如 if(position % 2 == 0){ (( ListView)parent).setDivider(getResource().getDrawable(R.drawable.deviderheight)); }其他{}
  • @Devu Soman 的意思是我想要顶部为白色,底部为黑色。
  • 参考stackoverflow.com/questions/6544723/… 并使用#ffffff 表示白色,#000000 表示黑色

标签: android android-layout listview


【解决方案1】:

有几种方法可以实现这一点。一种简单的方法是在列表中完全隐藏分隔线(将分隔线宽度设置为 0 或将分隔线设置为 null),并让每个列表项在顶部包含一行,在底部包含一行。考虑这个 xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="60dp">

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_alignParentTop="true"
        android:background="@android:color/white" />

    <LinearLayout ...>
        <!-- this is your current list item LinearLayout -->
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_alignParentBottom="true"
        android:background="@android:color/black" />

</RelativeLayout>

由于您已经在使用自定义适配器,因此只需在适配器的 getView 方法中为列表项布局使用上述 XML 即可。这将生成 60dp 高的列表项,顶部有一条白线,底部有一条黑线。

【讨论】:

  • 这也是一个很好的解决方案,但我的布局不是相对的而是线性的
  • @Nikhil 添加你的线性布局,我说“你的列表项布局的其余部分放在这里”。或者,发布您的列表项的布局,我会相应地更新我的答案。
  • @Aleka G 我正在用我的行设计更新帖子
  • @Nikhil 我更新了我的答案以使用您现有的列表项。
  • 不应该是一个RelativeLayout让layout_alignParentToBottom工作吗?
【解决方案2】:

您似乎想在列表视图的顶部和底部放置线条(边框)。如果这是你想要的,我会建议下面的代码。

按如下方式在res/values/styles.xml中准备一行。

<style name="Line">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">2px</item>
    <item name="android:layout_marginLeft">10dp</item>
    <item name="android:layout_marginRight">10dp</item>
    <item name="android:background">@drawable/line_gradient</item>
</style>

res/drawable/line_gradient.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
<gradient
    android:startColor="@color/white"
    android:endColor="@color/white"
    android:centerColor="@color/blue"
    android:useLevel="false"
    android:type="linear"
    android:angle="0"/>
</shape>

最后按以下方式在需要的地方使用该行。

 <View
        style="@style/Line"/>
<ListView
................
................
/>
 <View
        style="@style/Line"/>

这是我在应用程序中使用的代码,但您可以根据需要自定义或着色。甚至您可以直接在listview 的上方和下方创建视图。但是在 styles.xml 中单独创建一个会使其在多个地方使用而没有代码冗余。

【讨论】:

  • 但我想为列表视图中的每个项目提供它
  • 那么您可能需要在自定义行 xml 文件中的自定义行(列表项)之前和之后使用该视图,并将分隔线高度设置为 0dp。
【解决方案3】:

我不认为有办法做到这一点。但是您可以为您的ListView 添加footerview 和headerview。我想它会带来你想要的。

例如:

TextView tv1 = new Textview(this);
tv1.setBackgroundResource(R.drawable.dividerheight);

和

listview.addHeaderView(totalExpense);
listview.addFooterView(totalExpense);

希望对你有帮助。

【讨论】:

  • 但我想为列表视图中的每个项目提供它。
  • 每个列表视图意味着什么?您是指每个列表项吗?
  • 那么你可以使用devider。你的devider有什么问题?
  • @Gunasseelen 如何在底部给出。我尝试了 layout_below 属性,但它不起作用。最多的人只使用过这种方式。
猜你喜欢
  • 1970-01-01
  • 2011-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-14
相关资源
最近更新 更多