完整内容,请查看:http://www.zhdoc.net/android/reference/android/widget/Switch.html

Switch

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 "14.5sp". 
尺寸,带单位的一个浮点数,例如:14.5sp。 

android:switchPadding switch控件与说明文本之间的最小间距

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". 
尺寸,带单位的一个浮点数,例如:14.5sp。 

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。

Switch(Context context, AttributeSet attrs)

Construct a new Switch with default styling, overriding specific style attributes as requested.
构造一个默认样式的新Switch,其请求指定的样式属性覆盖。

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,其请求指定的样式属性覆盖。

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.
构造一个由指定的主题属性或样式资源决定的默认样式的新Switch,其请求指定的样式属性覆盖。

Public方法

void draw(Canvas c)

Manually render this view (and all of its children) to the given Canvas.
手动将此视图 (及其所有子项) 渲染到指定的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.
每当视图热点发生改变时,调用此方法,并需要传播到由视图管理的drawable或子视图。

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.
返回视图左内边距(padding)的大小,如果有左边Drawable的话,则加上它的空间。

int getCompoundPaddingRight()

Returns the right padding of the view, plus space for the right Drawable if any.
返回视图右内边距(padding)的大小,如果有右边Drawable的话,则加上它的空间。

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.
获取switch滑块的drawable ——这是一个可以按下并且在轨道内拖动的小块。

int getThumbTextPadding()

Get the horizontal padding around the text drawn on the switch itself.
获取switch文本的水平内边距(padding)。

ColorStateList getThumbTintList()
PorterDuff.Mode getThumbTintMode()
Drawable getTrackDrawable()

Get the drawable used for the track that the switch slides within.
获取轨道的drawable,其用于switch内部。

ColorStateList getTrackTintList()
PorterDuff.Mode getTrackTintMode()
void jumpDrawablesToCurrentState()

Call Drawable.jumpToCurrentState() on all Drawable objects associated with this view.
在与此视图关联的所有drawable对象上,调用Drawable.jumpToCurrentState()

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.
当从一个视图中获取辅助结构时调用, 作为auto-fill请求的一部分。

void onProvideStructure(ViewStructure structure)

Called when assist structure is being retrieved from a view as part of Activity.onProvideAssistData.
当从一个视图中获取辅助结构时调用, 作为Activity.onProvideAssistData的一部分。

boolean onTouchEvent(MotionEvent ev)

Implement this method to handle touch screen motion events.
实现此方法,以处理触摸屏的MotionEvent。

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.
设置switch控件的最小宽度(以像素为单位)。

void setSwitchPadding(int pixels)

Set the amount of horizontal padding between the switch and the associated text.
设置开关和关联文字之间的水平内边距(padding)。

void setSwitchTextAppearance(Context context, int resid)

Sets the switch text color, size, style, hint color, and highlight color from the specified TextAppearance resource.
设置指定TextAppearance资源中的switch控件的文本颜色,大小,样式,提示文本颜色和高亮颜色。

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.
设置在switch上显示的文字的字体和样式,如果你提供的字体没有所有的字符,那么将用绘制的粗斜体代替。

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.
设置switch滑块的drawable ——这是一个可以按下并且在轨道内拖动的小块。

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.
设置switch滑块的drawable ——这是一个可以按下并且在轨道内拖动的小块。

void setThumbTextPadding(int pixels)

Set the horizontal padding around the text drawn on the switch itself.
设置switch文本的水平内边距(padding)。

void setThumbTintList(ColorStateList tint)

Applies a tint to the thumb drawable.
滑块drawable的tint。

void setThumbTintMode(PorterDuff.Mode tintMode)

Specifies the blending mode used to apply the tint specified by setThumbTintList(ColorStateList)} to the thumb drawable.
设置混合模式,用于由setThumbTintList(ColorStateList)指定的滑块drawable的着色。

void setTrackDrawable(Drawable track)

Set the drawable used for the track that the switch slides within.
设置轨道的drawable,其用于switch内部。

void setTrackResource(int resId)

Set the drawable used for the track that the switch slides within.
设置轨道的drawable,其用于switch内部。

void setTrackTintList(ColorStateList tint)

Applies a tint to the track drawable.
用于滑轨drawable的tint。

void setTrackTintMode(PorterDuff.Mode tintMode)

Specifies the blending mode used to apply the tint specified by setTrackTintList(ColorStateList)} to the track drawable.
设置混合模式,用于由setTrackTintList(ColorStateList)指定的轨道drawable的着色。

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.
每当视图状态发生变化时, 都会调用此方法, 从而影响所显示的drawable的状态。

int[] onCreateDrawableState(int extraSpace)

Generate the new Drawable state for this view.
生成此视图的新Drawable状态。

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.
如果你的视图子类显示在其自己的drawable对象上,则应该重载此方法,并返回true,以显示它所显示的任何drawable的对象。

继承方法

完整内容,请查看:http://www.zhdoc.net/android/reference/android/widget/Switch.html

更多相关文章

  1. Android(安卓)设置声音时出现按键音
  2. 【Android】:跳转系统界面汇总
  3. android 调用摄像头
  4. Android(安卓)5.0 默认水波纹背景属性,可设置任何View
  5. Android(安卓)头像上传
  6. UI组件
  7. android引用外部工程或jar包
  8. ToggleButton
  9. Android(安卓)TextView 文字居中

随机推荐

  1. http请求不带referer的解决方法
  2. 如何查看HTTP接口的响应时间
  3. SEO优化中URL中的相对路径和绝对路径区别
  4. 不安全的HTTP方法
  5. 区块链交易所开发,合约交易模式搭建
  6. 区块链交易所:虚拟货币交易的5个技巧
  7. 三十分钟做一个网页游戏
  8. 开始着手用Python写一个游戏脚本
  9. 游戏开发完整学习路线,都在这里了
  10. Java文件上传实例并解决跨域问题