选项卡背景颜色图片的变化以及TextView加边框

来源:互联网 发布:淘宝商城过户 编辑:程序博客网 时间:2024/05/16 17:33

项目中需要实现点击当前选项卡,然后当前选项卡变色,之前选中的选项卡恢复原状。

我刚开始选择的方法很笨,就是如果选中,v1.set...v2.set....看起来可读性很差,

经过实践,现将我想的思路写出来,供大家参考。

还是先上图这是3个textview,由于只是讲如何换背景,这里就用简单的布局来实现。

效果就是这样。


布局文件,我就不贴了,就是3个textView 垂直排列,直接贴主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 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.id.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可以去掉你添加的所有背景色以及图片itemList.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);}}

像上面注释的一样,

android.R.color.transparent可以去掉你添加的所有背景色以及图片,将view.setBackgroundColor(Color.parseColor("#FF83FA"));注释换为

view.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_launcher));后

效果图如下:



接下来是为TextView设置边框

还是在这个项目里,我们先在drawable下添加settext_bg.xml

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" >    <!-- 背景色 -->    <solid android:color="#FFE4B5"/>    <!-- 边框色 -->    <stroke android:width="0.5dip" android:color="#81CE47" /></shape>

然后再布局文件中让textview 引用android:background="@drawable/settext_bg"

还是在这个项目中我们会看到下面的效果



我们发现初始化的时候有边框,点击变背景色的时候,边框没有了,而且点击后其他的什么的都没有了,

为了实现理想效果,我们先在drawable下添加settext_bg2.xml用来设置点击后当前textview的背景和边框

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" >    <!-- 背景色 -->    <solid android:color="#FF83FA"/>    <!-- 边框色 -->    <stroke android:width="0.5dip" android:color="#81CE47" /></shape>
然后修改changeOtherTextViewBackgroundColor(View view)方法

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);}
可以看到我们的效果实现了

以上就是全部内容,记录一下,省得遗忘。



1 0