//控件id
android:id = "@+id/xxx" @+id/xxx表示新增控件命名为xxx
//宽度与高度
android:layout_width="wrap_content" //wrap_content或者match_parent
android:layout_height="wrap_content" //wrap_content或者match_parent
//此外,可以具体设置高度和宽度显示的像素,不过这样设置如果图片尺寸大于设置的显示的尺寸,则图片是显示不全的,这是可以配合android:scaleType属性。
android:layout_width="200dp"
android:layout_height="200dp"
//把原图按照指定的大小在View中显示,拉伸显示图片,不保持原比例,填满ImageButton.
android:scaleType="fitXY"
//其他的关于android:scaleType的参数解释,也可以参考下面的直观图
//android:scaleType="center" 在视图中心显示图片,并且不缩放图片
//android:scaleType="centercrop" 按比例缩放图片,使得图片长 (宽)的大于等于视图的相应维度
//android:scaleType="centerinside" 按比例缩放图片,使得图片长 (宽)的小于等于视图的相应维度
//android:scaleType="fitcenter" 按比例缩放图片到视图的最小边,居中显示
//android:scaleType="fitend" 按比例缩放图片到视图的最小边,显示在视图的下部分位置
//android:scaleType="fitstart" 把图片按比例扩大/缩小到视图的最小边,显示在视图的上部分位置
//android:scaleType="matrix" 用矩阵来绘制
//图片来源,需要将图片复制放到res/drawable文件夹里面,引用的时候不需要写图片的后缀
android:src ="@drawable/beautiful">
5.Checkbox和RadioButton
android.widget. RadioButton单选按钮,继承自android.widget.CompoundButton,在android.widget包中
单选按钮要声明在RadioGroup,RadioGroup是流式布局android.widget.LinearLayout的子类。
单选按钮状态更改的监听,是要给他的RadioGroup添加:
setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener)监听器。
注意监听器类型和CheckBox是不一样的。
android:id="@+id/radio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
//设置RadioButton的排列方式,分为水平排列horizontal与垂直排列vertical
android:orientation="horizontal" >
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" //normal,bold,italic分别为正常,加粗以及斜体,默认为normal
/>
android:id="@+id/rd2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="上海" />
public class MainActivity extends Activity{
////对控件对象进行声明
private TextView textView;
private RadioGroup radiogroup;
private RadioButton radiobutton1;
private RadioButton radiobutton2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//通过控件的ID来得到代表控件的对象
textView = (TextView) findViewById(R.id.text_view);
radiogroup = (RadioGroup) findViewById(R.id.radio_group);
radiobutton1 = (RadioButton) findViewById(R.id.rd1);
radiobutton2 = (RadioButton) findViewById(R.id.rd2);
//调用setOnCheckedChangeListener来对RadioGroup进行监听的代码
radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId == radiobutton1.getId()){
textView.setText("北京");
}else if(checkedId == radiobutton2.getId()){
textView.setText("上海");
}
}
});
}
}
android.widget.CheckBox复选按钮,继承自android.widget.CompoundButton,在android.widget包中。
isChecked() :检查是否被选中
监听状态修改,需要添加:
setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener);
android:id="@+id/cb1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
//设置复选按钮后紧跟的文本提示文字
android:text="北京"
//设置文字的大小
android:textSize="30sp"
//设置文字的颜色
android:textColor="#0000FF"
//字体格式
android:textStyle="normal" //normal,bold,italic分别为正常,加粗以及斜体,默认为normal/>
android:id="@+id/cb2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="上海"
android:textSize="30sp"
android:textColor="#0000FF"/>
在mainactivity.java中监听按钮
public class MainActivity extends Activity{
////对控件对象进行声明
private TextView textView;
private CheckBox checkbox1;
private CheckBox checkbox2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//通过控件的ID来得到代表控件的对象
textView = (TextView) findViewById(R.id.text_view);
checkbox1 = (CheckBox) findViewById(R.id.cb1);
checkbox2 = (CheckBox) findViewById(R.id.cb2);
//为第一个 CheckBox 注册监听
checkbox1.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//如果第一个 CheckBox 被选中
if(isChecked == true){
textView.setText("CheckBox选中北京");
}
}
});
//为第二个 CheckBox 注册监听
checkbox2.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//如果第二个 CheckBox 被选中
if(isChecked == true){
textView.setText("CheckBox选中上海");
}
}
});
}
}
6.ImageView
ImageView控件负责显示图片,其图片的来源可以是在资源文件中的id,也可以是Drawable对象或者位图对象。还可以是Content Provider的URI。
//控件id
android:id = "@+id/xxx" @+id/xxx表示新增控件命名为xxx
//宽度与高度
android:layout_width="wrap_content" //wrap_content或者match_parent
android:layout_height="wrap_content" //wrap_content或者match_parent
//此外,可以具体设置高度和宽度显示的像素,不过这样设置如果图片尺寸大于设置的显示的尺寸,则图片是显示不全的,这是可以配合android:scaleType属性。
android:layout_width="200dp"
android:layout_height="200dp"
//把原图按照指定的大小在View中显示,拉伸显示图片,不保持原比例,填满ImageButton.
android:scaleType="fitXY"
//其他的关于android:scaleType的参数解释,也可以参考下面的直观图
//android:scaleType="center" 在视图中心显示图片,并且不缩放图片
//android:scaleType="centercrop" 按比例缩放图片,使得图片长 (宽)的大于等于视图的相应维度
//android:scaleType="centerinside" 按比例缩放图片,使得图片长 (宽)的小于等于视图的相应维度
//android:scaleType="fitcenter" 按比例缩放图片到视图的最小边,居中显示
//android:scaleType="fitend" 按比例缩放图片到视图的最小边,显示在视图的下部分位置
//android:scaleType="fitstart" 把图片按比例扩大/缩小到视图的最小边,显示在视图的上部分位置
//android:scaleType="matrix" 用矩阵来绘制
//图片来源,需要将图片复制放到res/drawable文件夹里面,引用的时候不需要写图片的后缀
android:src ="@drawable/beautiful">
7.ProgressBar
ProgressBar 用于在界面上显示一个进度条,表示我们的程序正在加载一些数据,运行程序,会看到屏幕中有一个圆形进度条正在旋转。
在布局xml文件中的用法非常简单:
android:id="@+id/pb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
//默认是圆形进度条,可以知道样式设置为水平进度条
style="?android:attr/progressBarStyleHorizontal"/>
//指定成水平进度条后,我们还可以通过 android:max属性给进度条设置一个最大值,然后在代码中动态地更改进度条的进度
android:max="100"
那么如何才能让进度条在数据加载完成时消失呢,这里我们就需要用一开始所讲的Android 控件的可见属性。
可以通过代码来设置控件的可见性,使用的是 setVisibility()方法,可以传入 View.VISIBLE、View.INVISIBLE 和 View.GONE 三种值。
下面实现点击一下按钮让进度条消失,再点击一下按钮让进度条出现的这种效果,这里只给出按钮监听的代码:
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//通过 getVisibility()方法来判断 ProgressBar 是否可见
if (progressBar.getVisibility() == View.GONE) {
progressBar.setVisibility(View.VISIBLE);
} else {
progressBar.setVisibility(View.GONE);
}
}
});
8DatePicker–日期与时间选择控件
常用方法:
getDayOfMonth():获取当前Day
getMonth():获取当前月
getYear()获取当前年
updateDate(int year, int monthOfYear, int dayOfMonth):更新日期
TimePicker
查看一个在24小时或上午/下午模式下一天的时间。
常用方法
setCurrentMinute(Integer currentMinute)设置当前时间的分钟
getCurrentMinute()获取当前时间的分钟
setEnabled(boolean enabled)设置当前视图是否可以编辑。
m_TimePicker.setIs24HourView(true);设置为24小时制显示
setOnTimeChangedListener(TimePicker.OnTimeChangedListener onTimeChangedListener)当时间改变时调用
相关包类:
TimePickerDialog、DatePickerDialog
以对话框形式显示日期时间视图
Calendar
日历是设定年度日期对象和一个整数字段之间转换的抽象基类,如,月,日,小时等。