2020-04-07 常用控件Button篇

目标:熟悉使用android系统自带的常用控件,


widgets:Button、ToggleButton、CheckBox、RadioButton、CheckedTextView、Spinner、ProgressBar、SeekBar、QuickContacBadge、RatingBar、Switch、Space






控件对应的图片(控件展示)

Button:

<Button

    android:text="@string/btn"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content" />

ToggleButton:

<ToggleButton

    android:id="@+id/tog_btn"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:textOff="@string/off"

    android:textOn="@string/on" />




默认状态

点击后状态



CheckBox:

<CheckBox

    android:id="@+id/checkbox"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content" />


默认未选中状态

选中状态


RadioButton:

<RadioButton

    android:id="@+id/radioButton1"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content" />


默认未点击状态

点击后状态


搭配RadioGroup使用,实现在RadioGroup中单选功能:

<RadioGroup

    android:id="@+id/radioGroup"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:layout_marginTop="10dp"

    android:background="@color/colorBackgroud"

    android:orientation="horizontal"

    android:weightSum="2">

<RadioButton

        android:id="@+id/radioButton1"

        android:layout_width="0dp"

        android:layout_height="wrap_content"

        android:layout_weight="1"

        android:gravity="center"

        android:text="@string/one"

        android:textColor="@color/colorAccent" />

<RadioButton

        android:id="@+id/radioButton2"

        android:layout_width="0dp"

        android:layout_height="wrap_content"

        android:layout_weight="1"

        android:gravity="center"

        android:text="@string/two"

        android:textColor="@color/colorPrimary" />

</RadioGroup>





CheckedTextView:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@android:id/text1"

    android:layout_width="match_parent"

    android:layout_height="?android:attr/listPreferredItemHeightSmall"

    android:textAppearance="?android:attr/textAppearanceListItemSmall"

    android:gravity="center_vertical"

    android:checkMark="?android:attr/listChoiceIndicatorSingle"

    android:paddingStart="?android:attr/listPreferredItemPaddingStart"

    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd" />


系统布局文件simple_list_item_single_choice和simple_list_item_multiple_choice默认引用了CheckedTextView 控件


Spinner:

<Spinner

    android:id="@+id/spinner"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:dropDownWidth="200dp"

    android:popupBackground="@color/colorAccent"

    android:spinnerMode="dialog" />//dropdown下拉



点击后展示一个新的布局,展示方式有两种,一:下拉展示,二:弹窗展示

下拉展示

弹窗展示:



ProgressBar:

样式分为两种,确定与不确定,确定的设置为直线进度条,不确定的设置为加载圆圈进度条

直线进度条:style="?android:attr/progressBarStyleHorizontal"

<ProgressBar

    style="?android:attr/progressBarStyleHorizontal"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:max="100"  //进度分为100份

    android:progress="100"//加载100份

    />



圆圈进度条:style="?android:attr/progressBarStyle"


SeekBar:

<SeekBar

    android:layout_width="match_parent"

    android:layout_height="wrap_content" />


常见的音视频播放进度条



QuickContacBadge:

QuickContactBadge,联系人快捷标识,具体效果为显示一张联系人图片,点一下弹出与之相关的快捷功能栏,如打电话、发短信等


<QuickContactBadge

    android:id="@+id/badge"

    android:src="@drawable/timg"

    android:layout_width="30dp"

    android:layout_height="30dp" />



模拟查找手机电话联系人:

badge=findViewById(R.id.badge);

badge.assignContactFromPhone("18153808579",false);

点击上图的图片后,出现:


点击所有联系人下的号码,出现:



RatingBar:

RatingBar是ProgressBar的一个延伸,ProgressBar的进度一般由代码控制,RatingBar的进度可以拖拽来控制。


基于宽度设置的属性:wrap_content,默认显示5个星星,


如果填充为铺满match_parent,则根据手机屏幕适配,填满整行个星星,

<RatingBar

    android:layout_width="match_parent"

    android:layout_height="wrap_content" />


改变星星图片属性,使用progressDrawable属性:

android:progressDrawable="@drawable/ratingbar_bg"


Switch:

<Switch

    android:id="@+id/swth"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:textOff="@string/off_switch"

    android:textOn="@string/on_switch" />

默认状态


点击后状态

Space:

Space 经常用于组件之间的缝隙,其draw()为空,减少了绘制渲染的过程。组件之间的距离使用 Space 会提高了绘制效率,特别是对于动态设置间距会很方便高效。

正是因为draw()为空,对该 view 没有做任务绘制渲染,所以不能对 Space 设置背景色。

<android.support.v4.widget.Space

    android:layout_width="1dp"

    android:layout_height="10dp" />


android:background="@color/colorAccent"//设置背景默认为系统的红色,发现背景并未变成红色,

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容