Tab background color image changes and TextView border
Click the current tab in the project, and the current tab changes color. The selected tab is restored to its original state.
The method I selected at the beginning is very stupid, that is, if it is selected, v1.set... v2.set... looks very poor in readability,
After practice, I will write down my ideas for your reference.
First, this is three textviews. As we only talk about how to change the background, we will use a simple layout here.
This is the result.
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140919/20140919083910209.jpg" alt = "\">
Layout file, I will not paste it, it is three textviews vertically arranged, directly paste the main activity
Package com. example. alltest; import java. util. arrayList; import java. util. list; import android. OS. bundle; import android. app. activity; import android. graphics. color; import android. view. view; import android. view. view. onClickListener; public class AllTestActivity extends Activity implements OnClickListener {private List
ItemList = new ArrayList
(); @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_all_test); findViewById (R. id. textView1 ). setOnClickListener (this); findViewById (R. id. textView2 ). setOnClickListener (this); findViewById (R. id. textView3 ). setOnClickListener (this);} public void onClick (View v) {// TODO Auto-generated method stubswitch (v. getId () {case R. I D. textView1: changeOtherTextViewBackgroundColor (v); break; case R. id. textView2: changeOtherTextViewBackgroundColor (v); break; case R. id. textView3: changeOtherTextViewBackgroundColor (v); break; default: break;} private void changeOtherTextViewBackgroundColor (View view) {// TODO Auto-generated method stubif (itemList! = Null) {for (int I = 0; I <itemList. size (); I ++) {// android. r. color. transparent can remove all background colors and itemList images you have added. get (I ). setBackgroundColor (getResources (). getColor (android. r. color. transparent);} itemList. clear ();} view. setBackgroundColor (Color. parseColor ("# FF83FA"); // view. setBackgroundDrawable (getResources (). getDrawable (R. drawable. ic_launcher); itemList. add (view );}}
As noted above,
Android. R. color. transparent can remove all background colors and images you have added, and change the comment to view. setBackgroundColor (Color. parseColor ("# FF83FA ").
View. setBackgroundDrawable (getResources (). getDrawable (R. drawable. ic_launcher );
As follows:
Next, set the border for TextView
In this project, add settext_bg.xml under drawable.
Then let textview reference android: background = "@ drawable/settext_bg" in the layout file"
In this project, we will see the following results:
We found that there was a border during initialization. When we clicked to change the background color, there was no border, and there was nothing else after we clicked,
To achieve the desired effect, we first add settext_bg2.xml under drawable to set the background and border of the current textview after clicking
Then modify the changeOtherTextViewBackgroundColor (View view) method.
private void changeOtherTextViewBackgroundColor(View view) {// TODO Auto-generated method stubif (itemList!=null) {for (int i = 0; i < itemList.size(); i++) {itemList.get(i).setBackgroundDrawable(getResources().getDrawable(R.drawable.settext_bg));}itemList.clear();}view.setBackgroundDrawable(getResources().getDrawable(R.drawable.settext_bg2));itemList.add(view);}We can see that our results have been achieved
The above is all the content, record it, and save the trouble of forgetting.