1. 程式人生 > >Android TextView 設定文字背景色或文字顏色

Android TextView 設定文字背景色或文字顏色

String str="這是設定TextView部分文字背景顏色和前景顏色的demo!";

int bstart=str.indexOf("背景");

int bend=bstart+"背景".length();

int fstart=str.indexOf("前景");

int fend=fstart+"前景".length();

SpannableStringBuilder style=new SpannableStringBuilder(str);

style.setSpan(new BackgroundColorSpan(Color.RED),bstart,bend,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  

style.setSpan(new ForegroundColorSpan(Color.RED),fstart,fend,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

TextView tvColor=(TextView) findViewById(R.id.tv_color);

tvColor.setText(style);

相關推薦

Android TextView 設定文字景色文字顏色

String str="這是設定TextView部分文字背景顏色和前景顏色的demo!"; int bstart=str.indexOf("背景"); int bend=bstart+"背景".length(); int fstart=str.indexOf("前景")

android TextView 設定部分文字景色文字顏色

通過SpannableStringBuilder來實現,它就像html裡邊的元素改變指定文字的文字顏色或背景色 public class MainActivity extends Activity { @Override protected void onCreate(Bund

androidTextView設定部分文字景色文字顏色

通過SpannableStringBuilder來實現,它就像html裡邊的元素改變指定文字的文字顏色或背景色 public class MainActivity extends Activity

Toolbar 設定 menu 景色、字型顏色以及彈出選單不懸浮

Toolbar 設定 menu 背景色及字型顏色 step1 定義xml <style name="ToolbarPopupTheme" parent="@style/ThemeOverl

Android自定義TextView 自定義設定圓角景色

public class RoundTextView extends TextView {  private int mBgColor = 0;  private int mCornerSize = 18; public RoundTextView(Context cont

android系統修改狀態列景色以及文字顏色

修改狀態列的顏色一般是android系統5.1以上才支援,程式碼如下 activity.getWindow().setStatusBarColor(activity.getResources().getColor(R.color.custom_status_bar_c

SecureCR 改變景色文字顏色

global ESS img bold linu 文件 info 快捷 就是 1.打開SecureCR鏈接Linux服務器,Options->Session Options->Emulation->Terminal 選擇Linux (相應的服務器系統)AN

Android Material適配 為控制元件設定指定景色和點選波紋效果

大部分時候,我們都需要為控制元件設定指定背景色和點選效果 4.x以下可以使用selector,5.0以上需要帶波紋效果,以下是實現該效果的方法。 比如實現Button的Material適配 <Button android:text

html,button的景色文字顏色

<button type="button" class="btn" style="background-color: #7ED321;width: 76px;heigh

android設定Activity景色為透明的2種方法

android設定背景色為透明 方法一: 只要在配置檔案內activity屬性配置內加上 android:theme="@android:style/Theme.Translucent"  就好了。 這樣就呼叫了android的透明樣式! 方法二: 先在res/values

unity3d ugui 文字景色

如圖。在專案中需要在文字後面顯示一個背景,背景的大小要隨文字的多少變動。 遍尋百度未曾獲得解決辦法,但是從momo的 UGUI研究院之Text文字漸變(十一)發現了繼承BaseMeshEffect可以獲取字元的網格資訊於是使用瞭如下解決辦法。 具體思想:

Android TextView圖文混排,圖片和文字居中對齊

TextView用來顯示文字是最普通的用法了,有些情況需要我們再文字中顯示圖片,比如顯示錶情,下邊來說說處理方法. 1.下圖是專案中一個UI需求 如圖需要在文字的前邊顯示新上標籤,開始的處理方法是用了兩個空間分別顯示標籤和文字,但是有個問題是第二行的文字

TextView設定為只顯示一行文字對顯示不完的用省略號替代

先看效果圖: TextView只顯示一行,對顯示不完的內容使用省略號替代。 就設定標記的2個屬性 android:lines="1"(設定文字最大的顯示行數) android:singleLin

TextView設定Lines和ellipsize後文字顯示不全

在xml設定 android:lines="1" android:ellipsize="end" 再在程式碼中設定setText內容後,內容不顯示 這時需要在程式碼中進行顯示控制 tv_cy_drug_colon.setEllipsize(TextUtils.Tr

Android 按壓狀態改變景色

stat oid select col cto drawable XML min mine Android 按壓狀態改變背景色 <selector xmlns:android="http://schemas.android.com/apk/res/android"&g

WPF之數據觸發器 改變控件景色閃爍

cti get 背景 anim soft reg using round markup 需求,很多矩形表示桶,其中:空桶=紅色,滿桶=綠色,使用中=紅綠閃爍。 <Window x:Class="FlickerDemo.MainWindow" xmln

181213 - 解決Android的應用APP景色突然被改變的問題

在魅族最新的特定版本出現APP背景突然被改變顏色的問題 出問題的機型相關資訊 型號:魅族16th Plus Android 版本: 8.1.0 安全補丁 版本: 2018年10月1日 Flyme 版本: 7.1.3.3A 問題詳情描述 APP整體背景 = #FFFF

android Textview設定單行顯示,多餘的顯示省略號

  singleline設定為true,則可以實現單行顯示 ellipsize則是控制省略號的位置,分別有start(開始),middle(中間),end(最後),marquee(跑馬燈)。 程式碼展示: <TextView android:

Android textview 設定字型樣式

public void setFontSanFrancisco(Context context, TextView textView) { Typeface typeface = Typeface.createFromAsset(context.getAssets(), "SanFran

Android RadioButton自定義景色圓角樣式的方法

先看圖,實現如下樣式: 來看該radiobutton的程式碼: <RadioButton android:id="@+id/rb_a" android:layout_width="0dip"