Setting TextView TextAppeareance Programmatically in android

前端 未结 2 1728
面向向阳花
面向向阳花 2021-02-04 23:12

I am going to implement a LinearLayout in which the input fields are programmatically generated according to the number of fields of the database table.

Unf

2条回答
  •  无人及你
    2021-02-04 23:54

    Use like this. It will work.

    textView.setTextAppearance(this, android.R.style.TextAppearance_Large);
    

    Or, since API 23, you don't need to pass a context. Hence, you can simply call:

    textView.setTextAppearance(android.R.style.TextAppearance_Large);
    

    If you want to support API 23 or higher as well as lower one, you can use the below method to simplify your task. Use the below method only if you are already targeting API 23 or higher. If you are targeting API is less than 23, the below code will give error as the new method wasn't available in it.

    public void setTextAppearance(Context context, int resId) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            super.setTextAppearance(context, resId);
        } else {
            super.setTextAppearance(resId);
        }
    }
    

提交回复
热议问题