Android基础入门-UI组件


UI组件分类

在这里插入图片描述




常见UI组件

TextView及其子类
Textview文本框显示文本内容的文本区域,不可编辑。
EditText编辑框显示文本内容,可编辑修改
Button按钮
RadioButton:单选按钮,一般和RadioGroup一起使用组成选项组
CheckBox:复选按钮
ToggleButton:状态开关按钮
Switch:开关

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
        <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="This is TextView"/>
        <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:text="This is EditView"/>
        <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="This is Button"/>
        <RadioButton android:layout_width="match_parent" android:layout_height="wrap_content" android:text="This is RadioButton"/>
        <CheckBox android:layout_width="match_parent" android:layout_height="wrap_content" android:text="This is CheckBox"/>
        <ToggleButton android:layout_width="match_parent" android:layout_height="wrap_content" android:text="This is ToggleButton"/>
        <Switch android:layout_width="match_parent" android:layout_height="wrap_content" android:text="This is Switch"/>
</LinearLayout>

运行效果如下图
在这里插入图片描述




进度条

在Android中,提供了进度条,拖动条和星级评分条等进度条类组件。

  • ProgressBar进度条
    • 进度条可以动态地显示进度,避免用户死等的尴尬状态。用颜色填充表明进度(不允许用户拖动)
<ProgressBar	属性列表	>	</ProgressBar>
XML属性如下:
android:max	用于设置进度条的最大值
android:progress	指定进度条已完成的进度值
android:progressDrawable	进步条轨道的绘制形式
style属性能指定风格:
?android:attr/progressBarStyleHorizontal	细的长条水平进度条
?android:attr/progressBarStyleLarge(Small)	大(小)圆形进度条
@android:style/Widget.ProgressBar.Large(Small)	大(小)跳跃、旋转画面的进度条
  • SeekBar拖动条
    • 允许用户拖动滑块来改变值,因此拖动条通常用于对系统的某种数值进行调节,亮度调节的底层就是它。
      由于拖动条可以被用户控制,需要添加OnSeekBarChangeListener监听器
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
	public void onStopTrackingTouch(SeekBar seekBar){
		//要执行的代码
	}
	public void onStartTrackingTouch(SeekBar seekBar){
		//要执行的代码
	}
	public void onProgressChanged(SeekBar seekBar,int progress,boolean fromUser){
		//要执行的代码,参数progress表示当前进度。
	}
)
  • RatingBar星级评分条
    • 允许用户通过拖动来改变进度,不过RatingBar通过星星来表示进度。
<RatingBar	属性列表>	</RatingBar>
XML属性:
android:isIndicator	指定星级评分条是否允许用户改变,true为不允许
android:numStars	总共有多少星
android:rating		默认星级
android:stepSize	每次最少改变多少星级
方法:
getRating():	获取等级
getStepSize():	获取每次改变最少的值
getProgress():	获取进度

效果如下图所示
在这里插入图片描述




图像类组件

<ImageView	属性列表>	</ImageView>
XML属性:
android:adjustViewBounds	是否调整自己的边界保持长宽比
android:maxHieght			设置最大高度,需要上式属性值为true
android:maxWidth			设置最大宽度
android:src					所显示的Drawable对象的ID
android:tint				用于为图片着色

网格视图

<GridView	属性列表>	</GridView>
XML属性:
android:columnWidth			用于设置列的宽度
android:gravity				设置对齐方式
android:horizontalSpacing	设置各元素水平间距
android:numColumns			设置列数
android:verticleSpacing		设置垂直元素间距

通常使用Adapter类为GridView组件提供数据(XML只写一个GridView,数据由Adapter绑定)

  • ArrayAdapter 数组适配器,将数组多个值封装成多个列表项.
  • SmipleAdapter 简单适配器,将List集合多个值封装多个列表项.
  • BaseAdapter 抽象类,继承它需要实现较多方法



下拉列表框Spinner

android:spinnerMode:列表显示的模式,有两个选择,为弹出列表(dialog)以及下拉列表(dropdown),如果不特别设置,为下拉列表。。
android:entries:使用<string-array…/>资源配置数据源。
android:prompt="@string/info" 指定下拉标题
AdapterView.OnItemCLickListener:列表项被点击时触发。
AdapterView.OnItemLongClickListener:列表项被长按时触发。
AdapterView.OnItemSelectedListener:列表项被选择时触发。

<Spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:entries="@array/spingarr"
android:spinnerMode="dialog"
></Spinner>

string.xml

<resources>
    <string name="app_name">HelloWorld</string>
    <string-array name="spingarr">
        <item>北京</item>
        <item>上海</item>
        <item>广州</item>
        <item>深圳</item>
    </string-array>
</resources>

如果需要在用户选择不同列表项后,执行相应的处理,则可以为该下拉列表添加OnItemSelectedListener事件监听器,用getItemAtPosition()方法获取选中的值,用Toast.makeText()获取值显示出来,代码如下

	Spinner spinner = (Spinner)	findViewById(R.id.spinner);
	//创建监听事件.
	spinner.setOnItemSeletedListener(new AdapterView.OnItemSelectedListener(){
		public void OnItemSeleted(AdapterView<?> parent,View view,int position,long id){
			String result = parent.getItemAtPosition(postion).toString();	//获取选择的值
			Toast.makeText(MainActivity.this,result,Toast.LENGTH_SHORT).show();	//显示选中的值
		}
	});

运行结果
在这里插入图片描述




列表视图

Listview允许用户通过手指上下滑动的方式将屏幕外的数据滚动到屏幕内,同时屏幕原有的数据会滚动出屏幕
列表视图(ListView)是Android中最常用一种视图,它以垂直列表的形式列出需要显示的列表项(如联系人)

<ListView	属性列表>	</ListView>
android:divider	为列表视图设置分隔条,颜色分隔.
android:dividerHeight	分隔条的高度
android:entries			数组资源ListView指定列表项

既然ListView适用于展示大量数据,那么应该先将数据提供好。数据可以是数据库中,也可以是网上下载,这里我用的是数组,数组中的数据无法直接传递给ListView,因此需要借助适配器来完成。

	private String[] data = {"Apple","Banana"};
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContenView(R.layout.activity_main);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.listview1,data);
		ListView listView = (ListView) findViewById(R.id.listView);
		listView.setAdapter(adapter);
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值