public class Switch
extends CompoundButton
java.lang.Object | |||||
↳ | android.view.View | ||||
↳ | android.widget.TextView | ||||
↳ | android.widget.Button | ||||
↳ | android.widget.CompoundButton | ||||
↳ | android.widget.Switch |
A Switch is a two-state toggle switch widget that can select between two options. The user may drag the "thumb" back and forth to choose the selected option, or simply tap to toggle as if it were a checkbox. The text
property controls the text displayed in the label for the switch, whereas the off
and on
text controls the text on the thumb. Similarly, the textAppearance
and the related setTypeface() methods control the typeface and style of label text, whereas the switchTextAppearance
and the related setSwitchTypeface() methods control that of the thumb.
Switch 是一个可以选择两种选项的双态控件。用户可以拖动“滑块(thumb)”来回选择所选的选项,或简单地点击切换,把它当做一个单选框(checkbox)。这个text
属性控制着在switch中显示的文本, 而设置 off
(文本)和 设置on
( 文本)控制在滑块(thumb)上显示的文本。同样,在textAppearance
及相关setTypeface() 方法里控制着字体和文本样式的标签,而 switchTextAppearance
及相关setSwitchTypeface()控制着滑块(thumb)。
SwitchCompat
is a version of the Switch widget which runs on devices back to API 7.SwitchCompat
这个版本的Switch控件运行在 API 7 的设备中。
See the Toggle Buttons guide.
请参考Toggle Buttons。
XML属性 |
|
---|---|
android:showText |
是否绘制on/off文本。 |
android:splitTrack |
是否拆分轨道(track), 为滑块(thumb)的drawable留下一个缺口。 |
android:switchMinWidth |
switch控件的最小宽度 May be a dimension value, which is a floating point number appended with a unit such as " |
android:switchPadding |
switch控件与说明文本之间的最小间距 May be a dimension value, which is a floating point number appended with a unit such as " |
android:switchTextAppearance |
switch控件滑块上显示的文本的TextAppearance样式。 |
android:textOff |
当开关处于未选中/“关闭”状态时使用的文本。 |
android:textOn |
当开关处于选中/“开启”状态时使用的文本。 |
android:textStyle |
文本样式(粗体,斜体,粗斜体) |
android:thumb |
来回切换滑块(thumb)的图片资源(Drawable) |
android:thumbTextPadding |
switch控件的滑块内的文本两侧的内边距(padding)。 |
android:thumbTint |
用于滑块的tint。 |
android:thumbTintMode |
混合模式,用于滑块着色。 |
android:track |
switch控件的滑轨drawable,滑块在这个滑轨中。 |
android:trackTint |
用于滑轨的tint。 |
android:trackTintMode |
混合模式,用于滑轨着色。 |
android:typeface |
文本字体(标准字体, sans字体, serif字体, monospace字体) |
继承XML属性 |
|
---|---|
From class android.widget.CompoundButton
|
|
From class android.widget.TextView
|
|
From class android.view.View
|
继承常量 |
---|
From class android.widget.TextView
|
From class android.view.View
|
继承字段 |
---|
From class android.view.View
|
Public构造方法 |
|
---|---|
Switch(Context context) Construct a new Switch with default styling. |
|
Switch(Context context, AttributeSet attrs) Construct a new Switch with default styling, overriding specific style attributes as requested. |
|
Switch(Context context, AttributeSet attrs, int defStyleAttr) Construct a new Switch with a default style determined by the given theme attribute, overriding specific style attributes as requested. |
|
Switch(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) Construct a new Switch with a default style determined by the given theme attribute or style resource, overriding specific style attributes as requested. |
Public方法 |
|
---|---|
void |
draw(Canvas c) Manually render this view (and all of its children) to the given Canvas. |
void |
drawableHotspotChanged(float x, float y) This function is called whenever the view hotspot changes and needs to be propagated to drawables or child views managed by the view. |
CharSequence |
getAccessibilityClassName() Return the class name of this object to be used for accessibility purposes. |
int |
getCompoundPaddingLeft() Returns the left padding of the view, plus space for the left Drawable if any. |
int |
getCompoundPaddingRight() Returns the right padding of the view, plus space for the right Drawable if any. |
boolean |
getShowText() |
boolean |
getSplitTrack() Returns whether the track should be split by the thumb. |
int |
getSwitchMinWidth() Get the minimum width of the switch in pixels. |
int |
getSwitchPadding() Get the amount of horizontal padding between the switch and the associated text. |
CharSequence |
getTextOff() Returns the text displayed when the button is not in the checked state. |
CharSequence |
getTextOn() Returns the text displayed when the button is in the checked state. |
Drawable |
getThumbDrawable() Get the drawable used for the switch "thumb" - the piece that the user can physically touch and drag along the track. |
int |
getThumbTextPadding() Get the horizontal padding around the text drawn on the switch itself. |
ColorStateList |
getThumbTintList() |
PorterDuff.Mode |
getThumbTintMode() |
Drawable |
getTrackDrawable() Get the drawable used for the track that the switch slides within. |
ColorStateList |
getTrackTintList() |
PorterDuff.Mode |
getTrackTintMode() |
void |
jumpDrawablesToCurrentState() Call |
void |
onMeasure(int widthMeasureSpec, int heightMeasureSpec) Measure the view and its content to determine the measured width and the measured height. |
void |
onProvideAutoFillStructure(ViewStructure structure, int flags) Called when assist structure is being retrieved from a view as part of an auto-fill request. |
void |
onProvideStructure(ViewStructure structure) Called when assist structure is being retrieved from a view as part of |
boolean |
onTouchEvent(MotionEvent ev) Implement this method to handle touch screen motion events. |
void |
setChecked(boolean checked) Changes the checked state of this button. |
void |
setShowText(boolean showText) Sets whether the on/off text should be displayed. |
void |
setSplitTrack(boolean splitTrack) Specifies whether the track should be split by the thumb. |
void |
setSwitchMinWidth(int pixels) Set the minimum width of the switch in pixels. |
void |
setSwitchPadding(int pixels) Set the amount of horizontal padding between the switch and the associated text. |
void |
setSwitchTextAppearance(Context context, int resid) Sets the switch text color, size, style, hint color, and highlight color from the specified TextAppearance resource. |
void |
setSwitchTypeface(Typeface tf) Sets the typeface in which the text should be displayed on the switch. |
void |
setSwitchTypeface(Typeface tf, int style) Sets the typeface and style in which the text should be displayed on the switch, and turns on the fake bold and italic bits in the Paint if the Typeface that you provided does not have all the bits in the style that you specified. |
void |
setTextOff(CharSequence textOff) Sets the text displayed when the button is not in the checked state. |
void |
setTextOn(CharSequence textOn) Sets the text displayed when the button is in the checked state. |
void |
setThumbDrawable(Drawable thumb) Set the drawable used for the switch "thumb" - the piece that the user can physically touch and drag along the track. |
void |
setThumbResource(int resId) Set the drawable used for the switch "thumb" - the piece that the user can physically touch and drag along the track. |
void |
setThumbTextPadding(int pixels) Set the horizontal padding around the text drawn on the switch itself. |
void |
setThumbTintList(ColorStateList tint) Applies a tint to the thumb drawable. |
void |
setThumbTintMode(PorterDuff.Mode tintMode) Specifies the blending mode used to apply the tint specified by |
void |
setTrackDrawable(Drawable track) Set the drawable used for the track that the switch slides within. |
void |
setTrackResource(int resId) Set the drawable used for the track that the switch slides within. |
void |
setTrackTintList(ColorStateList tint) Applies a tint to the track drawable. |
void |
setTrackTintMode(PorterDuff.Mode tintMode) Specifies the blending mode used to apply the tint specified by |
void |
toggle() Change the checked state of the view to the inverse of its current state |
保护方法 |
|
---|---|
void |
drawableStateChanged() This function is called whenever the state of the view changes in such a way that it impacts the state of drawables being shown. |
int[] |
onCreateDrawableState(int extraSpace) Generate the new |
void |
onDraw(Canvas canvas) Implement this to do your drawing. |
void |
onLayout(boolean changed, int left, int top, int right, int bottom) Called from layout when this view should assign a size and position to each of its children. |
boolean |
verifyDrawable(Drawable who) If your view subclass is displaying its own Drawable objects, it should override this function and return true for any Drawable it is displaying. |
完整内容,请查看:http://www.zhdoc.net/android/reference/android/widget/Switch.html