选项卡背景颜色图片的变化以及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
- 选项卡背景颜色图片的变化以及TextView加边框
- 自己设置textview的边框和背景以及圆形背景
- iOS-UIView加边框以及边框颜色
- android里TextView加下划线的几种方式 以及 为TextView添加边框
- Android TextView边框和背景,以及圆形背景
- Android TextView边框和背景,以及圆形背景
- Android实现连续并排的若干个TextView单击改变背景颜色达到选项卡Tab栏切换效果
- 给textview获得焦点时加边框背景
- Android如何让TextView子元素随着父元素的背景变化而改变字体颜色?
- Textview中字体颜色和背景同时变化
- 圆角边框,渐变背景的Textview
- android在代码中四种设置控件(以及TextView的文字颜色)背景颜色的方法
- android在代码中四种设置控件(以及TextView的文字颜色)背景颜色的方法
- 关于android中改变TextView背景颜色以及文字颜色的问题
- textview加边框
- textView加边框
- textview加边框
- TextView加边框
- Java 枚举举例讲解
- 20140918学习遇到的小问题
- 由sizeof和strlen想到的
- 游戏运营笔记八 迷你西游国庆节活动策划
- fragment动态改变actionbar的item
- 选项卡背景颜色图片的变化以及TextView加边框
- Android之单元测试学习
- 粒子发射系统属性备忘
- Oracle列的预定义命令column
- php笔记之web应用中引用自定义的php文件
- C++ Primer视频教程(初级、中级、高级全套价值千余元)
- automake问题
- 正则表达式
- 详解 android layout布局属性