需求:
京東APP在設(shè)置頁面,有“字體設(shè)置”功能,點(diǎn)擊進(jìn)去后可以設(shè)置大字號(hào)和標(biāo)準(zhǔn)字號(hào)呻拌,經(jīng)產(chǎn)品和開發(fā)協(xié)商,我們這邊的需求定為:在APP中增加一個(gè)類似于京東的設(shè)置頁面睦焕,點(diǎn)擊設(shè)置后APP中指定頁面的指定文案變成大字號(hào)藐握,每個(gè)文案放大的比例不一定相同靴拱,產(chǎn)品會(huì)規(guī)定好每個(gè)文案的原始字號(hào)和放大后的字號(hào)。
實(shí)現(xiàn):
自定義一個(gè)TextView,在xml中聲明需要放大到的字號(hào)猾普,通過一個(gè)本地?cái)?shù)據(jù)庫(kù)中存儲(chǔ)的值來判斷是否需要放大袜炕,如果需要,就在自定義的TextView中重新設(shè)置字號(hào)初家;根據(jù)產(chǎn)品的需求在需要放大文案的頁面使用自定義TextView.
ZoomTextView全部代碼
package com.example.myapplication;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.TextView;
import androidx.annotation.Nullable;
public class ZoomTextView extends TextView {
public ZoomTextView(Context context) {
super(context);
}
public ZoomTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
TypedArray ta=context.obtainStyledAttributes(attrs,R.styleable.ZoomTextView);
float scal_to_textSize = ta.getDimensionPixelSize(R.styleable.ZoomTextView_scal_to_textSize, -1);//要縮放到的字號(hào)
ta.recycle();
if (scal_to_textSize > 0 && Word.isScal) //表示xml中設(shè)置了縮放比例
{
setTextSize(TypedValue.COMPLEX_UNIT_PX,scal_to_textSize);
}
}
}
attrs文件代碼
<declare-styleable name="ZoomTextView">
<attr name="scal_to_textSize" format="dimension"/>
</declare-styleable>
xml代碼
<com.example.myapplication.ZoomTextView
android:layout_marginTop="30dp"
android:id="@+id/tv_zoom"
android:textSize="15dp"
android:textColor="#000000"
app:scal_to_textSize="18dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本" />
開關(guān)類代碼
package com.example.myapplication;
public class Word {
public static boolean isScal = true; //真正項(xiàng)目中可以用保存到本地?cái)?shù)據(jù)庫(kù)中的字段值代替偎窘,以實(shí)現(xiàn)永久化設(shè)置
}