English 中文(简体)
How do I remove lines between ListViews on Android?
原标题:

I m using two ListViews like this:

<ListView
   android:id="@+id/ListView"
   android:text="@string/Website"
   android:layout_height="30px"
   android:layout_width="150px"
   android:scrollbars="none"
   android:transcriptMode="normal"/>
<ListView
   android:id="@+id/ListView1"
   android:text="@string/Website"
   android:layout_height="30px"
   android:layout_width="150px"
   android:scrollbars="none"
   android:transcriptMode="normal"/>

There is one blank line between the two ListViews. How do I remove it?

问题回答

To remove the separator between items in the same ListView, here is the solution:

getListView().setDivider(null);
getListView().setDividerHeight(0);

developer.android.com # ListView

Or, if you want to do it in XML:

android:divider="@null"
android:dividerHeight="0dp"
  1. If you want to remove a divider line, use this code:

    android:divider="@null"
    
  2. If you want to add a space instead of a divider line:

    android:divider="@android:color/transparent"
    android:dividerHeight="5dp"
    

So, you can use any drawable or color in the divider attribute.

There are different ways to achieve this, but I m not sure which one is the best (I don t even know is there is a best way). I know at least two different ways to do this in a ListView:

1. Set divider to null:

1.1. Programmatically

yourListView.setDivider(null);

1.2. XML

This goes inside your ListView element.

android:divider="@null"

2. Set divider to transparent and set its height to 0 to avoid adding space between listview elements:

2.1. Programmatically:

yourListView.setDivider(new ColorDrawable(android.R.color.transparent));
yourListView.setDividerHeight(0);

2.2. XML

android:divider="@android:color/transparent"
android:dividerHeight="0dp"

Set divider to null:

JAVA

  listview_id.setDivider(null);

XML

<ListView 
  android:id="@+id/listview"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:divider="@null"
  />

In XML:

android:divider="@null"

Or in Java:

listView.setDivider(null);
   <ListView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/list"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:divider="@null"
  android:dividerHeight="0dp"/>

You can put below property in listview tag

android:divider="@null"

(or) programmatically listview.Divider(null); here listview is ListView reference.

Or in XML:

android:divider="@drawable/list_item_divider"
android:dividerHeight="1dp"

You can use a color for the drawable (e.g. #ff112233), but be aware, that pre-cupcake releases have a bug in which the color cannot be set. Instead a 9-patch or a image must be used..

You can try the following. It worked for me...

android:divider="@android:color/transparent"
android:dividerHeight="0dp" 

I find it easier to implement it in the XML file as it can be harder to trace the line of code in a class with hundreds of lines. For the XML you can use "null":

android:divider="@null"

For ListFragment use

getListView().setDivider(null)

after the list has been obtained.

If you want to remove lines from

⛔ Problem

Having lines between items from <ListView>

✅ Solution

add an attribute android:drivider="@null"

enter image description here

If this android:divider="@null" doesn t work, maybe changing your ListViews for Recycler Views? 

String txt = ( (TextView) view).getText().toString();
adapter.remove(txt);
  

enter image description here





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签