简体   繁体   中英

Android studio: change textSize on Custom extends activity

I extended the TextView class to "CustomTextView" that's why I needed to set a custom font. So it's the result:

public class CustomTextView extends TextView {

    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/nn.otf"));
        this.setTextSize(30);
    }
}

You can see that I setted a default textSize that it's 30:

When I want this CustomTextView, I use this code:

<com.calendar.CustomTextView
    android:id="@+id/edData"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="DOM 21 GIU"
    android:textSize="45dp"/>

If you notice, I setted the textSize value to 45dp, but the it remains 30 (from the custom class). How do I set a different textSize? Also, for bold style?

you should remove the this.setTextSize(30); from the constructor, because the xml layout do the resize in during the super(context, attrs) call, and the bold font should be included in the otf file (usually are different otf files for different styles)

This is my solution: (I made tests with textColor and not textSize, but it's the same).

I edited the res/values/atrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomTextView">
        <attr name="textColor" format="string" />
    </declare-styleable>
</resources>

Then, I edited my class like:

public class CustomTextView extends TextView {

    public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs);
    }

    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public CustomTextView(Context context) {
        super(context);
        init(null);
    }

private void init(AttributeSet attrs) {
    if (attrs != null) {
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CustomTextView);
        String fontName = a.getString(R.styleable.CustomTextView_fontName);

        if (textColor != null) {
            this.setTextColor(Color.parseColor(textColor));
        } else {
            this.setTextColor(Color.parseColor("#000000"));
        }

        a.recycle();
    }
   }

}

So this work:

 <com.imgspa.listviewadapter.CustomTextView
        android:id="@+id/ed"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            listviewadapter:textColor="#FF0000"/>

        <com.imgspa.listviewadapter.CustomTextView
            android:id="@+id/edRis"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="35dp" />

The first custom textview text is red, the second one is black

To set the style just use android:textStyle="bold" . For your other problem, you can check the textSize from the attributes I believe using attrs.getAttribute(NAME OF ATTRIBUTE) method and then you can set it to that value or set it to 30 depending on what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM