Android控件的后花园

作者: 瓦西大人 | 来源:发表于2018-06-19 20:58 被阅读56次

写在前面的话
正所谓“拳不离手,曲不离口”,今天答辩的时候我就遇到了一个特别尴尬的事情。在老师问我上滑功能的控件是什么的时候,我一时语塞,想不出到底是什么来,发觉自己基础实在是薄弱得很,所以今天总结一下Android的常用控件以备日后随时巩固学习。

学海无涯.jpg
一、常用控件
1.TextView(文本显示,不可编辑)
常见属性有:
//id
android:id = "@+id/textview"  

//宽度与高度
android:layout_width="wrap_content"  
android:layout_height="wrap_content"  

//文本文字 
android:text="@string/hello_world" 

//字体大小
android:textSize="16sp"  

//字体颜色
android:textColor="#FFFFFF" 

//字体格式
android:textStyle="normal"  

//文本显示位置
android:gravity="center"  //文字对齐方式

2.EditText(文本交互,可编辑)
常见属性与TextView相同,不同的有:

//输入内容设置为password类型
android:password="true"  

//输入内容设置为phoneNumber类型
android:phoneNumber="true" 

//设定光标为显示/隐藏
android:cursorVisible = "false|true" 

3.Button
用户单击 Button 来触发事件,然后为 Button 注册监听器,实现 监听。
4.ImageButton
以图片作为背景的按钮
常见属性:

//图片来源
android:src ="@drawable/img"

5.ImageView图片控件
6.RadioButton,RadioGroup
RadioButton(单选按钮)它是一种单个圆形单选框双状态的按钮,可以选择或不选择。

RadioGroup (单选组合框)用于 将 RadioButton 框起来。在多个 RadioButton被 RadioGroup 包含的情况下,同一时刻只可以选择一个 RadioButton,并用 setOnCheckedChangeListener 来对 RadioGroup 进行监听
常见属性:

 <RadioGroup
        android:id="@+id/radio_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        //排列方式,水平排列horizontal,垂直排列vertical
        android:orientation="horizontal" >
           <RadioButton 
               android:id="@+id/rd1"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               //设置单选后紧跟的文本提示文字
               android:text="北京"
               //设置文字的大小
               android:textSize="30sp"
               //设置文字的颜色
               android:textColor="#0000FF"
               //字体格式
               android:textStyle="normal"  
                />
           <RadioButton 
               android:id="@+id/rd2"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:textSize="30sp"
               android:text="大连" />
      </RadioGroup>

7.CheckBox
CheckBox(复选按钮)先在布局文件中定义多选按钮, 然后对每一个多选按钮进行事件监听 setOnCheckedChangeListener,通过 isChecked 来判断 选项是否被选中,做出相应的事件响应。
二、流行的个性化控件
1.ListView
将一系列的数据进行列表式展示。
由于ListView是以列表的形式显示数据,所以我们需要事先将数据准备好,这些数据可以来自本地数据库或者来自网络。但这些数据是不能直接给ListView显示的,需要借助适配器来完成。Android中提供可很多的适配器的实现类。例如ArrayAdapter,这个类通过泛型来提供各种数据类型的支持,然后在构造方法中将我们的数据传入。
2.RecyclerView
与ListView原理类似,但RecyclerView简化了数据的展示和处理,使用LayoutManager来确定每一个item的排列方式。,为增加和删除项目提供默认的动画效果(可以定义自己的LayoutManager和添加删除动画)。

recyclerView =(RecyclerView)findViewById(R.id.recyclerView);  
LinearLayoutManager layoutManager = new LinearLayoutManager(this );  
//设置布局管理器  
recyclerView.setLayoutManager(layoutManager);  
//设置为垂直布局,这也是默认的  
layoutManager.setOrientation(OrientationHelper. VERTICAL);  
//设置Adapter  
recyclerView.setAdapter( recycleAdapter);  
 //设置分隔线  
recyclerView.addItemDecoration( new DividerGridItemDecoration(this ));  
//设置增加或删除条目的动画  
recyclerView.setItemAnimator( new DefaultItemAnimator());  

作为可以代替ListView的控件,这个控件使用简单,效率较高,并且实现在support库中,在各个版本之间的兼容性比较好。
2.ProgressBar
进度条,用于向用户展示某个耗时操作完成的进度,而不让用户感觉是程序失去了响应,从而更好地提升用户界面的友好性。
常用属性:

<ProgressBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="?android:attr/progressBarStyleHorizontal"
    //第一显示进度
    android:max="100"
   //最大显示进度
    android:progress="50"
    android:secondaryProgress="80"
    android:indeterminate="false" />

关键方法:
setProgress(int) 设置第一进度
setSecondaryProgress(int) 设置第二进度
getProgress() 获取第一进度
getSecondaryProgress() 获取第二进度
incrementProgress(int) 增加或减少第一进度
incrementSecondaryProgress(int) 增加或减少第二进度
getMax() 获取最大进度
3.ScrollView
ScrollView滚动视图是指当拥有很多内容,屏幕显示不完时,需要通过滚动跳来显示的视图。
ScrollView只支持垂直滚动。
4.Gallery
Gallery用于横向显示图像列表

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <!-- android:unselectedAlpha: 设置未选中的条目的透明度(Alpha)。该值必须是float类型,比如:“1.2”。 -->
    <Gallery android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:spacing="10dip" 
        android:unselectedAlpha="1.2"
        android:id="@+id/gallery"
        android:layout_marginTop="30dp"/>
    <!-- ImageSwitcher 组件,在单击某一个Gallery组件中的图像时在下方显示一个放大的图像 -->
    <ImageSwitcher android:id="@+id/imageSwitcher"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp" />
</LinearLayout>

三、推荐博客
仅仅掌握以上控件还是远远不够的,安卓的发展是不断进步的,所以需要更多的知识充实自己的大脑,所以仔细选择了两篇优秀的博客以供参考。
1.Android重要控件大全:https://blog.csdn.net/mapeifan/article/details/50454002
包括流行控件及优秀项目源码地址,可以参考学习一下。
2.最流行的Android组件大全:
https://blog.csdn.net/qq_21445563/article/details/50945318
包括流行组件最详细的使用方法,适合开发时使用。
结语
菜鸟一枚,总结上会有些许不足,请多多指教,欢迎分享交流。

相关文章

网友评论

    本文标题:Android控件的后花园

    本文链接:https://www.haomeiwen.com/subject/gyrzeftx.html