在Android中,不管什么樣子的視圖都是繼承自View類,因此我們必須要自定義一個(gè)View類,下面看一下代碼實(shí)現(xiàn)
創(chuàng)新互聯(lián)公司專注于企業(yè)全網(wǎng)整合營(yíng)銷推廣、網(wǎng)站重做改版、隆安網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、成都h5網(wǎng)站建設(shè)、成都商城網(wǎng)站開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為隆安等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
關(guān)于Android中View控件的分類可以分為以下幾類:
1. 文本類:
TextView、EditText、AutoCompleteTextView、MultAutoCompletTextView 、(TextSwitcher) 、(DigitalClock)
ExtractEditText、CheckedTextView、Chronometer
2.按鈕類:
Button、CheckBox、RadioButton(RadioGroup) 、ToggleButton 、(ImageButton ) CompoundButton
2. 縮放按鈕:
ZoomButton、ZoomControls
3. 圖片類:
ImageView、ZoomButton、ImageButton、(ImageSwitcher ) QuickContactBadge
4. 時(shí)間控件:
DigitalClock、AnalogClock、TimePicker、DatePicker
5.進(jìn)度顯示:
ProgressBar、AbsSeekBar、SeekBar、RatingBar(星星評(píng)分)
6.導(dǎo)航: TabHost、TabWidget。
7.視頻媒體:
VideView、MediaController
8.Dialog對(duì)話框
CharacherPickerDialog、AlertDialog、DatePickerDialog、ProgressDialog、TimePickerDialog
9. 布局類控件:
AbsoluteLayout、LinearLayout、RadioGroup 、TableLayout、 TableRow、RelativeLayout、FrameLayout
10.需要適配器的布局類:
AdapterView、AbsListView、GridView、ListView、AbsSpinner、Gallery Spinner
11.滾動(dòng)條: HorizontalScrollView、ScrollView
12.網(wǎng)頁(yè): WebView
13.動(dòng)畫: ViewAimator、ViewFilpper、ViewSwitcher、ImageSwitcher、TextSwitcher
三星部分手機(jī)支持隱藏導(dǎo)航條功能(以三星Note8為例)。設(shè)置-顯示-導(dǎo)航條-顯示和隱藏按鈕-滑動(dòng)開關(guān)。開啟后,在下方導(dǎo)航欄左側(cè)出現(xiàn)一個(gè)小圓點(diǎn)的按鈕,點(diǎn)擊此圖標(biāo)可以隱藏導(dǎo)航欄。隱藏后,您可以通過(guò)從屏幕底部向上滑動(dòng)來(lái)使用導(dǎo)航按鈕。
溫馨提示:導(dǎo)航欄在某些屏幕上將始終顯示,無(wú)法隱藏。如:主屏幕、相機(jī)、微信、QQ和支付寶等。
做IOS開發(fā)的都知道,IOS提供了一個(gè)具有動(dòng)態(tài)開關(guān)效果的UISwitch組件,這個(gè)組件很好用效果相對(duì)來(lái)說(shuō)也很絢麗,當(dāng)我們?nèi)c(diǎn)擊開關(guān)的時(shí)候有動(dòng)畫效果,但遺憾的是Android上并沒有給我們提供類似的組件(聽說(shuō)在Android4.0的版本上提供了具有動(dòng)態(tài)效果的開關(guān)組件,不過(guò)我還沒有去看文檔),如果我們想實(shí)現(xiàn)類似的效果那該怎么辦了呢?看來(lái)又得去自定義了。
公司的產(chǎn)品最近一直在做升級(jí),主要做的就是把界面做的更絢麗更美觀給用戶更好的體驗(yàn)(唉,顧客是上帝......),其中的設(shè)置功能中就有開關(guān)按鈕,原來(lái)的開關(guān)做的是兩幅圖片,通過(guò)點(diǎn)擊圖片來(lái)給開關(guān)設(shè)置不同的狀態(tài)圖片,但是這種效果很死板和程序的整體風(fēng)格不太協(xié)調(diào),于是就想著實(shí)現(xiàn)類似于IOS中的開關(guān)效果。
拿著筆在圖紙上畫了畫,我實(shí)現(xiàn)的原理也是采用了兩幅圖片,一個(gè)整體的背景圖:和一個(gè)小滑塊圖:,用小滑塊圖實(shí)現(xiàn)在背景圖上滑動(dòng),當(dāng)小滑塊滑到左邊時(shí)恰好遮擋了開字,就是顯示的關(guān)了,同樣原理當(dāng)小滑塊滑動(dòng)到右側(cè)時(shí)恰好遮擋了關(guān)字也就是現(xiàn)實(shí)開了,滑動(dòng)的實(shí)現(xiàn)主要用的就是TranslateAnimation類,如有對(duì)TranslateAnimation不太熟悉的,問(wèn)問(wèn)度娘,她那有關(guān)TranslateAnimation的解說(shuō)多了去了,畢竟自己動(dòng)手,豐衣食足嘛,(*^__^*) 嘻嘻……
好了,老規(guī)矩來(lái)看一下項(xiàng)目結(jié)構(gòu)吧:
工程中switch_button.xml文件就是對(duì)應(yīng)的SwitchButton的布局文件,內(nèi)容不需要解釋,你一看就懂
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
?xml version="1.0" encoding="utf-8"?
LinearLayout
xmlns:android=""
android:id="@+id/switch_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/switch_bg"
ImageView
android:id="@+id/switch_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/switch_btn" /
/LinearLayout
SwitchButton的布局文件中根節(jié)點(diǎn)是個(gè)LinearLayout,把它的背景設(shè)置成了一個(gè)含有開關(guān)文字的圖片,里邊包含一個(gè)ImageView,這個(gè)ImageView就是用來(lái)在LinearLayout中進(jìn)行滑動(dòng)的。
其中自定義開關(guān)組件就是都在wedgit包下的SwitchButton,那么趕緊來(lái)看一下SwitchButton的代碼吧
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
public class SwitchButton extends LinearLayout {
/**
* 開關(guān)圖片
*/
private LinearLayout switchParent;
/**
* 滑塊圖片
*/
private ImageView switchButton;
/**
* 按鈕狀態(tài),默認(rèn)關(guān)閉
*/
private boolean isOn = false;
/**
* 滑塊需要滑動(dòng)的距離
*/
private int scrollDistance;
/**
* 開關(guān)按鈕監(jiān)聽器
*/
private SwitchChangedListner listner;
public SwitchButton(Context context) {
super(context);
initWedgits(context);
}
public SwitchButton(Context context, AttributeSet attrs) {
super(context, attrs);
initWedgits(context);
}
/**
* 初始化組件
*
* @param context
* 上下文環(huán)境
*/
private void initWedgits(Context context) {
try {
View view = LayoutInflater.from(context).inflate(
R.layout.switch_button, this);
switchParent = (LinearLayout) view.findViewById(R.id.switch_parent);
switchButton = (ImageView) view.findViewById(R.id.switch_button);
addListeners();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 添加事件監(jiān)聽器
*/
private void addListeners() {
try {
switchParent.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
isOn = !isOn;
scrollSwitch();
if (null != listner) {
// 開關(guān)開發(fā)或者關(guān)閉的回調(diào)方法
listner.switchChanged(getId(), isOn);
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 滑動(dòng)開關(guān)
*/
private void scrollSwitch() {
// 獲取滑塊需要滑動(dòng)的距離,滑動(dòng)距離等于父組建的寬度減去滑塊的寬度
scrollDistance = switchParent.getWidth() - switchButton.getWidth();
// 初始化滑動(dòng)事件
Animation animation = null;
if (isOn) {
animation = new TranslateAnimation(0, scrollDistance, 0, 0);
} else {
animation = new TranslateAnimation(scrollDistance, 0, 0, 0);
}
// 設(shè)置滑動(dòng)時(shí)間
animation.setDuration(200);
// 滑動(dòng)之后保持狀態(tài)
animation.setFillAfter(true);
// 開始滑動(dòng)
switchButton.startAnimation(animation);
}
/**
* 獲取開關(guān)狀態(tài)
*
* @return 【true:打開】【false:關(guān)閉】
*/
public boolean isOn() {
return isOn;
}
/**
* 設(shè)置開關(guān)狀態(tài)
*
* @param isOn
* 開關(guān)狀態(tài)【true:打開】【false:關(guān)閉】
*/
public void setOn(boolean isOn) {
if (this.isOn == isOn) {
return;
}
this.isOn = isOn;
post(new Runnable() {
@Override
public void run() {
scrollSwitch();
}
});
}
/**
* 設(shè)置開關(guān)狀態(tài)監(jiān)聽器
*
* @param listner
* 開關(guān)狀態(tài)監(jiān)聽器
*/
public void setOnSwitchListner(SwitchChangedListner listner) {
this.listner = listner;
}
/**
* 開關(guān)狀態(tài)監(jiān)聽器
*
* @author llew
*
*/
public interface SwitchChangedListner {
/**
* 開關(guān)狀態(tài)改變
*
* @param viewId
* 當(dāng)前開關(guān)ID
* @param isOn
* 開關(guān)是否打開【true:打開】【false:關(guān)閉】
*/
public void switchChanged(Integer viewId, boolean isOn);
}
}
本文題目:android開關(guān)按鈕,android 開關(guān)按鈕
分享地址:http://aaarwkj.com/article42/dsiisec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、網(wǎng)站設(shè)計(jì)公司、企業(yè)網(wǎng)站制作、、建站公司、域名注冊(cè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)