Android - 用java 給textview 設置文本內容顏色,背景色,背景圖片,Drawable資源


 

#給textView 添加文字顏色
    setTextColor(0xFF0000FF);  
    //0xFF0000FF是int類型的數據,分組一下0x|FF|0000FF,0x是代表顏色整 數的標記,ff是表示透明度,0000FF表示顏色,注意:這里0xFF0000FF必須是8個的顏色表示,不接受0000FF這種6個的顏色表示。 
    setTextColor(Color.rgb(255, 255, 255));  
    setTextColor(Color.parseColor("#FFFFFF"));    
    //還有就是使用資源文件進行設置  
    setTextColor(this.getResources().getColor(R.color.blue));  
    //通過獲得資源文件進行設置。根據不同的情況R.color.blue也可以是R.string.blue或者  
    //另外還可以使用系統自帶的顏色類  
    setTextColor(android.graphics.Color.BLUE); 


#給textView 添加背景色,背景圖片
    setBackgroundResource:通過顏色資源ID設置背景色。
    setBackgroundColor:通過顏色值設置背景色。
    setBackgroundDrawable:通過Drawable對象設置背景色。

    下面分別演示如何用這3個方法來設置TextView組件的背景
    setBackgroundResource方法設置背景:
    textView.setBackgroundResource(R.color.background);

    setBackgroundColor方法設置背景:
    textView.setBackgroundColor(android.graphics.Color.RED);

    setBackgroundDrawable方法設置背景:
    Resources resources=getBaseContext().getResources();
    Drawable drawable=resources.getDrawable(R.color.background);
    textView.setBackgroundDrawable(drawable);


#給Textview設置Drawable圖標
    參考:https://github.com/Carson-Ho/Search_Layout/blob/master/searchview/src/main/java/scut/carson_ho/searchview/EditText_Clear.java

    // 作用:在EditText上、下、左、右設置圖標(相當於android:drawableLeft=""  android:drawableRight="")
    // 注1:setCompoundDrawablesWithIntrinsicBounds()傳入的Drawable的寬高=固有寬高(自動通過getIntrinsicWidth()& getIntrinsicHeight()獲取)
    // 注2:若不想在某個地方顯示,則設置為null
    // 此處設置了左側搜索圖標

    // 另外一個相似的方法:setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)介紹
    // 與 setCompoundDrawablesWithIntrinsicBounds()的區別:可設置圖標大小
    // 傳入的Drawable對象必須已經setBounds(x,y,width,height),即必須設置過初始位置、寬和高等信息
    // x:組件在容器X軸上的起點 y:組件在容器Y軸上的起點 width:組件的長度 height:組件的高度

示例是給標簽文本設置圖標

....

searchRecordsLabel.setLabels(searchRecordList, new LabelsView.LabelTextProvider<SearchWordBean>() {
            @Override
            public CharSequence getLabelText(TextView label, int position, SearchWordBean data) {

                // return null;

                // label就是標簽項,在這里可以對標簽項單獨設置一些屬性,比如文本樣式等。
                Drawable wordDrawable = getResources().getDrawable(R.drawable.main_ic_12_hot);

                label.setCompoundDrawablesWithIntrinsicBounds(wordDrawable, null, null, null);

                //根據data和position返回label需要顯示的數據。
                return data.getName();
            }
        });
        
....

trendingRecordsLabel.setLabels(searchTrendingList, new LabelsView.LabelTextProvider<SearchWordBean>() {
            @Override
            public CharSequence getLabelText(TextView label, int position, SearchWordBean data) {

                // return null;

                // label就是標簽項,在這里可以對標簽項單獨設置一些屬性,比如文本樣式等。
                Drawable wordDrawable = getResources().getDrawable(R.drawable.main_ic_12_hot);
                wordDrawable.setBounds(0, 0, wordDrawable.getMinimumWidth(), wordDrawable.getMinimumHeight());

                label.setCompoundDrawables(wordDrawable, null, null, null);

                //根據data和position返回label需要顯示的數據。
                return data.getName();
            }
        });
...

 

 

kt java 管理 drawable

https://juejin.cn/post/7054466262889398280

https://www.cnblogs.com/guanxinjing/category/1475716.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM