文章目录
TextView 文本组件 显示简单的文本
ToggleButton 开关按钮
Edittext 文本组件
Button 按钮
CheckBox 复选框
Radiobutton+ RadioGroup 单选框
ImageVoew 图片
进度条组件 Progressbar的使用
…
一、布局设为线性布局
把根布局换位较为简单的线性布局Linelayout
选择convert view
选择线性布局
二、TextView
<TextView
android:id="@+id/tv_title"
android:text="这是TextView的内容"
android:textSize="25sp"
android:textStyle="bold"
android:textColor="#ff0000"
android:background="#999999"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- android:textAlignment="center"-->
<!-- 文本尺寸的大小-->
<!--bold 加粗 italic 斜体 normal 正常-->
<!--字体的颜色 RGB的形式-->
<!--字体文件末尾显示-->
<!--背景颜色设置为灰色-->
<!-- 边距大小-->
效果:
三、EditText
<EditText
android:id="@+id/er_phone_Number"
android:hint="请输入手机号码"
android:inputType="phone"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- 输入数据的类型 -->
效果:
四、Button
<Button
android:id="@+id/btn_submit"
android:text="提交"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
效果:
五、ImageView
添加图片
复制一张图片(png格式)
转换为工程目录
Ctrl v 粘贴
改名字确认就可以
<ImageView
android:id="@+id/iv_image"
android:src="@drawable/lock"
android:layout_gravity="center"
android:background="@android:drawable/picture_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<!-- source图片的来源-->
<!--重心 图片方式居中-->
<!-- 系统给的安卓的相框 -->
效果:
六、CheckBox
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="爱好:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<CheckBox
android:id="@+id/cb_foot"
android:text="足球"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<CheckBox
android:id="@+id/cb_basket"
android:text="篮球"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<CheckBox
android:id="@+id/cb_swimming"
android:text="游泳"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_ok"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
效果:
七、RadioButton + RadioGroup
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="性别:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioGroup
android:id="@+id/rg_gender"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/rb_male"
android:text="男"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:id="@+id/rb_female"
android:text="女"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RadioGroup>
</LinearLayout>
效果:
八、ToggleButton
建立资源文件夹
初始界面
编写代码
<ToggleButton
android:id="@+id/tb_test"
android:background="@drawable/selector"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ToggleButton>
效果:
开状态
关状态
九、ProgressBar
1.圆形进度条
<ProgressBar
android:id="@+id/progressbar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:text="正在加载中..."
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
效果:
2.水平进度条
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ProgressBar
android:id="@+id/pb_horizontal"
android:progress="30"
android:padding="10dp"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<SeekBar
android:id="@+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_progress"
android:text="当前的进度是:"
android:textSize="16sp"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
效果:
3.进度条数据绑定
初始化试图和数据绑定
/**
* 初始化视图
*/
public void initView(){
// 数据绑定
pbHorizontal = findViewById(R.id.pb_horizontal);
seekBar = findViewById(R.id.seekbar);
tvProgress = findViewById(R.id.tv_progress);
/* 可拖动进度条的改变事件 */
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// 滑竿改变
// 设置水平进度条的进度和滑竿的进度一样
pbHorizontal.setProgress(progress);
tvProgress.setText("当前进度为:" + progress + "%"); // 设置文本显示的和进度条一致
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// 拖动进度条完成
if(pbHorizontal.getProgress() >= pbHorizontal.getMax()){ // 当前进度条的值大于进度条的最大值
pbHorizontal.setVisibility(View.GONE); // 进度条消失
}
else
{
pbHorizontal.setVisibility(View.VISIBLE); // 进度条可见
}
}
});
}
效果:
拖到100%进度条消失
今天又起来晚了,摸摸弄弄来到就快九点球了,一上午又过去了。