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.
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 <View> itemList = new ArrayList <View> (); @ Override public void onCreate (Bundle savedInstanc EState) {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. id. textView1: changeOtherTextViewBackgroundColor (v); bre Ak; 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.
<? Xml version = "1.0" encoding = "UTF-8"?> <Shape xmlns: android = "http://schemas.android.com/apk/res/android"> <! -- Background color --> <solid android: color = "# FFE4B5"/> <! -- Border color --> <stroke android: width = "0.5dip" android: color = "#81CE47"/> </shape>
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
<? Xml version = "1.0" encoding = "UTF-8"?> <Shape xmlns: android = "http://schemas.android.com/apk/res/android"> <! -- Background color --> <solid android: color = "# FF83FA"/> <! -- Border color --> <stroke android: width = "0.5dip" android: color = "#81CE47"/> </shape>
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.