문제

I used TextView#getMaxLines() in my application for a few weeks without incident.

Lint is now informing me that it's only available in API 16+ (#setMaxLines() is API 1+...), though (to the best of my knowledge) I haven't modified anything that would cause this sudden flag - my min-sdk has been 8 for a while, and I have files in my source control to prove it.

1) Why could lint be flagging this error randomly? (To be clear, I mean to say that it should have caught it initially - I'm not implying this is something that it shouldn't have flagged at all).

2) Is there any way to retrieve the maxLines for a TextView on pre-api 16 devices? I checked the source but couldn't devise a way to retrieve this value using the exposed methods on a 2.2 device.

도움이 되었습니까?

해결책

A simpler solution was added to the support lib v4 inTextViewCompat

int maxLines = TextViewCompat.getMaxLines(yourtextView);

Check out this answer for some more informations.

다른 팁

You can use Reflection:

Field mMaximumField = null;
Field mMaxModeField = null;
try {
    mMaximumField = text.getClass().getDeclaredField("mMaximum");
    mMaxModeField = text.getClass().getDeclaredField("mMaxMode");
} catch (NoSuchFieldException e) {
    e.printStackTrace();
}

if (mMaximumField != null && mMaxModeField != null) {
    mMaximumField.setAccessible(true);
    mMaxModeField.setAccessible(true);

    try {
        final int mMaximum = mMaximumField.getInt(text); // Maximum value
        final int mMaxMode = mMaxModeField.getInt(text); // Maximum mode value

        if (mMaxMode == 1) { // LINES is 1
            text.setText(Integer.toString(mMaximum));
        }
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

OR:

Maybe, the best way is keep maxLine value at values and set it value in xml, and get as int resource in code.

The code for that method simply doesn't exist on 2.2, so you can't use it directly of course.

On the other hand, I've run a diff on the two files and it seems as though the new 4.2.2 TextView isn't using any new APIs internally (this is based solely on its imports). You may be able to add it as a class in your project and use it instead of the inbuilt TextView across all version of Android.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top