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.