第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在運(yùn)行時更改TextView的樣式

如何在運(yùn)行時更改TextView的樣式

慕尼黑8549860 2019-12-12 10:10:54
我有一個Android應(yīng)用程序,當(dāng)用戶點(diǎn)擊時TextView,我想應(yīng)用定義的樣式。我以為找到一個,textview.setStyle()但它不存在。我試過了textview.setTextAppearance();但它不起作用。
查看完整描述

3 回答

?
慕沐林林

TA貢獻(xiàn)2016條經(jīng)驗(yàn) 獲得超9個贊

我通過創(chuàng)建一個新的XML文件res/values/style.xml來做到這一點(diǎn),如下所示:


<?xml version="1.0" encoding="utf-8"?>

<resources>


    <style name="boldText">

        <item name="android:textStyle">bold|italic</item>

        <item name="android:textColor">#FFFFFF</item>

    </style>


    <style name="normalText">

        <item name="android:textStyle">normal</item>

        <item name="android:textColor">#C0C0C0</item>

    </style>


</resources>

我的“ strings.xml”文件中也有一個條目,如下所示:


<color name="highlightedTextViewColor">#000088</color>

<color name="normalTextViewColor">#000044</color>

然后,在我的代碼中,我創(chuàng)建了一個ClickListener來捕獲該TextView上的tap事件: 編輯: 自API 23起,不建議使用setTextAppearance


    myTextView.setOnClickListener(new View.OnClickListener() {

                public void onClick(View view){

                    //highlight the TextView

                    //myTextView.setTextAppearance(getApplicationContext(), R.style.boldText);

    if (Build.VERSION.SDK_INT < 23) {

       myTextView.setTextAppearance(getApplicationContext(), R.style.boldText);

    } else {

       myTextView.setTextAppearance(R.style.boldText);

    }

     myTextView.setBackgroundResource(R.color.highlightedTextViewColor);

                }

            });

要將其改回,可以使用以下方法:


if (Build.VERSION.SDK_INT < 23) {

    myTextView.setTextAppearance(getApplicationContext(), R.style.normalText);

} else{

   myTextView.setTextAppearance(R.style.normalText);

}

myTextView.setBackgroundResource(R.color.normalTextViewColor);



查看完整回答
反對 回復(fù) 2019-12-13
?
烙印99

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超13個贊

就像喬納森(Jonathan)建議的那樣,在使用textView.setTextTypeface作品的同時,我只是在幾秒鐘前在一個應(yīng)用程序中使用過它。


textView.setTypeface(null, Typeface.BOLD); // Typeface.NORMAL, Typeface.ITALIC etc.


查看完整回答
反對 回復(fù) 2019-12-13
?
收到一只叮咚

TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超5個贊

以編程方式:運(yùn)行時


您可以使用setTypeface()以編程方式進(jìn)行操作:


textView.setTypeface(null, Typeface.NORMAL);      // for Normal Text

textView.setTypeface(null, Typeface.BOLD);        // for Bold only

textView.setTypeface(null, Typeface.ITALIC);      // for Italic

textView.setTypeface(null, Typeface.BOLD_ITALIC); // for Bold and Italic

XML:設(shè)計(jì)時間


您還可以設(shè)置XML:


android:textStyle="normal"

android:textStyle="normal|bold"

android:textStyle="normal|italic"

android:textStyle="bold"

android:textStyle="bold|italic"

希望這會有所幫助


總結(jié)



查看完整回答
反對 回復(fù) 2019-12-13
  • 3 回答
  • 0 關(guān)注
  • 322 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號