爲之後能夠溫習本身作過的一些特效例子,同時也沒有必要再一次重寫代碼,特此從項目中收集代碼,按鈕顏色變化狀態以下:java
要實現如圖效果,首先要編寫一個佈局xml文件,代碼以下:android
<LinearLayoutide
android:layout_width="match_parent"佈局
android:layout_height="50dp"this
android:background="#ffffff"spa
android:orientation="horizontal" >3d
<LinearLayoutxml
android:id="@+id/ll1"blog
android:layout_width="100dp"utf-8
android:layout_height="32dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="12dp"
android:background="@drawable/shape_cart"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我愛"
android:textColor="#666666"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll2"
android:layout_width="100dp"
android:layout_height="32dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="12dp"
android:background="@drawable/shape_cart"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="妳的"
android:textColor="#666666"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll3"
android:layout_width="100dp"
android:layout_height="32dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="12dp"
android:background="@drawable/shape_cart"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="美麗善良"
android:textColor="#666666"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
窗體java代碼以下:
public class MainActivity extends Activity implements OnClickListener {
private LinearLayout ll1, ll2, ll3; //套住文本標籤的佈局
private TextView tv1, tv2, tv3; //文本標籤
//靜態常亮隨着程序的關閉而消失,若是要永久的記住顏色能夠存儲在本地
public static String seller_id = "0", available = "0",canorder="0";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
initView();
}
private void initView(){
ll1 = (LinearLayout) findViewById(R.id.ll1);
ll1.setOnClickListener(this);
tv1 = (TextView) findViewById(R.id.tv1);
ll2 = (LinearLayout) findViewById(R.id.ll2);
ll2.setOnClickListener(this);
tv2 = (TextView) findViewById(R.id.tv2);
ll3 = (LinearLayout) findViewById(R.id.ll3);
ll3.setOnClickListener(this);
tv3 = (TextView) findViewById(R.id.tv3);
//設置顏色
if (seller_id.equals("1") ) {
ll1.setBackgroundResource(R.drawable.shape_cart3);
tv1.setTextColor(this.getResources()
.getColor(R.color.tab_light));
} else if (seller_id.equals("0")|| seller_id.equals("")) {
ll1.setBackgroundResource(R.drawable.shape_cart);
tv1.setTextColor(this.getResources().getColor(R.color.txt6));
}
if (available.equals("1") ) {
ll2.setBackgroundResource(R.drawable.shape_cart3);
tv2.setTextColor(this.getResources()
.getColor(R.color.tab_light));
} else if (available.equals("0")|| available.equals("")) {
ll2.setBackgroundResource(R.drawable.shape_cart);
tv2.setTextColor(this.getResources().getColor(R.color.txt6));
}
if (canorder.equals("1") ) {
ll3.setBackgroundResource(R.drawable.shape_cart3);
tv3.setTextColor(this.getResources()
.getColor(R.color.tab_light));
} else if (canorder.equals("0")|| canorder.equals("")) {
ll3.setBackgroundResource(R.drawable.shape_cart);
tv3.setTextColor(this.getResources().getColor(R.color.txt6));
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ll1:
if (seller_id.equals("0") || seller_id.equals("")) {
ll1.setBackgroundResource(R.drawable.shape_cart3);
tv1.setTextColor(this.getResources()
.getColor(R.color.tab_light));
seller_id = "1";
} else if (seller_id.equals("1")) {
ll1.setBackgroundResource(R.drawable.shape_cart);
tv1.setTextColor(this.getResources().getColor(R.color.txt6));
seller_id = "0";
}
break;
case R.id.ll2:
if (available.equals("0") || available.equals("")) {
ll2.setBackgroundResource(R.drawable.shape_cart3);
tv2.setTextColor(this.getResources()
.getColor(R.color.tab_light));
available = "1";
} else if (available.equals("1")) {
ll2.setBackgroundResource(R.drawable.shape_cart);
tv2.setTextColor(this.getResources().getColor(R.color.txt6));
available = "0";
}
break;
case R.id.ll3:
if (canorder.equals("0") || canorder.equals("")) {
ll3.setBackgroundResource(R.drawable.shape_cart3);
tv3.setTextColor(this.getResources()
.getColor(R.color.tab_light));
canorder = "1";
} else if (canorder.equals("1")) {
ll3.setBackgroundResource(R.drawable.shape_cart);
tv3.setTextColor(this.getResources().getColor(R.color.txt6));
canorder = "0";
}
break;
default:
break;
}
}
}
邊框文件以下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="1dp"
android:color="#db2725" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<corners android:radius="3dp"/>
<solid android:color="#ffffff" />
</shape>
shape:設置邊框的狀態(這裏是矩形)
stroke:邊框線
padding:內邊距
corners :邊框角的設置
solid :實體(邊框的背景色)
還有一個 shape 文件修改 stroke 的顏色便可,用於文字顏色設置的 colors 文件可自定義。