How to set the split line color in ListView when Android fails

Source: Internet
Author: User

How to set the split line color in ListView when Android fails

Use the following code to set the separator line color. If it is invalid, the split line is invisible. Even if the split line width is set to 100, the split line cannot be seen. From the two lines of code, there should be no problem, it is also a common method.

        listView.setDividerHeight(1);        listView.setDivider(new ColorDrawable(Color.GRAY));

Why is it invalid? Let's look at the source code of setDivider.

    public void setDivider(Drawable divider) {        if (divider != null) {            mDividerHeight = divider.getIntrinsicHeight();        } else {            mDividerHeight = 0;        }        mDivider = divider;        mDividerIsOpaque = divider == null || divider.getOpacity() == PixelFormat.OPAQUE;        requestLayout();        invalidate();    }

Have you seen the following code?

        if (divider != null) {            mDividerHeight = divider.getIntrinsicHeight();

If the parameter is not blank, you need to obtain an internal default height. What is the height? refer to the following code:

    public int getIntrinsicHeight() {        return -1;    }

The height returns-1. It's time to see the line!

From the source code, as long as the setDivider interface of ListView is called, mDividerHeight is set to 0 or-1, so you cannot see the line at all. If you want to see the line, you need to reverse the call order, as follows:

        listView.setDivider(new ColorDrawable(Color.GRAY));        listView.setDividerHeight(1);

Here, I do not comment on the source code, but I personally think that this logic is really inappropriate. Setting the color should not change the basic parameters that have already been set.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.