How to properly set line height for Android?

前端 未结 6 1370
礼貌的吻别
礼貌的吻别 2020-12-13 03:37

I am a UX architect working with a team of Android developers that are mostly junior. We are having issues properly setting line height in Android.

We are using the

6条回答
  •  心在旅途
    2020-12-13 03:55

    Here's a way to do it programatically.

    public static void setLineHeight(TextView textView, int lineHeight) {
        int fontHeight = textView.getPaint().getFontMetricsInt(null);
        textView.setLineSpacing(dpToPixel(lineHeight) - fontHeight, 1);
    }
    
    public static int dpToPixel(float dp) {
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        float px = dp * (metrics.densityDpi / 160f);
        return (int) px;
    }
    

提交回复
热议问题