Android 動態設定控制項文字顏色和背景色

2021-08-03 17:49:26 字數 1032 閱讀 8170

一般我們要實現乙個控制項按壓變背景色,我們會寫selector的xml檔案,比如:

/*** 控制項背景色選擇器

** @param context   當前上下文

* @param idnormal  預設

* @param idpressed 按壓時

* @return

*/public static statelistdrawable setselector(context context, int idnormal, int idpressed) , pressed);

bg.addstate(new int, normal);

bg.addstate(new int{}, normal);

return bg;

}/**

* 控制項字型顏色選擇器

** @param mcontext 當前上下文

* @param normal   預設顯示顏色

* @param pressed  按壓後顯示的顏色

* @return

*/public static colorstatelist createcolorstatelist(context mcontext, int normal, int pressed) ;

int states = new int[6];

states[0] = new int;

states[1] = new int;

states[2] = new int;

states[3] = new int;

states[4] = new int;

states[5] = new int{};

colorstatelist colorlist = new colorstatelist(states, colors);

return colorlist;}

在activity中呼叫也很方便,如這樣:

Android 動態設定控制項寬高

android中動態載入控制項是常有的事,下面來記錄一下動態載入控制項 imageview new imageview this windowmanager wm this.getwindowmanager int width wm.getdefaultdisplay getwidth 螢幕寬度 i...

Android 動態設定控制項的布局

在android開發中,常常會動態地生成些控制項,並調調整其布局。那麼,如何動態調整空間的布局,請參閱下面的關鍵 mrlmain relativelayout findviewbyid r.id.rlmain layoutparams layoutparams new layoutparams an...

android動態生成控制項

方法 呼叫布局容器的addview,addview需傳入的引數就是你想放置的view 這裡給出乙個例子 final linearlayout layout new linearlayout this layout.setorientation linearlayout.vertical setcon...