View
- Processing text content TextView
- View Button that was clicked
- View ImageView that handles image content
- View EditView that receives user information input
- VIew ProgressBar of the progress bar class
General attributes
Attributes | Commonly used optional values |
---|---|
android:layout_width android:layout_height |
match_parent fill the entire container wrap_content contents is determined according to a positive integer unit exact size dp |
android:id | @id/valName Use the existing id @+id/valName to add a new id |
android:layout_margin | The distance between a positive integer dp and adjacent controls or edges |
android:padding | Positive integer dp The distance between the control content and the edge of the control |
android:background | Hexadecimal color value color as background @mipmap/resourceId image as background |
android:layout_gravity android:gravity |
android:layout_gravity relative to the position of the parent container android:gravity control content position center_horizontal center_vertical center left right bottom top |
android:visibility | visible state invisible invisible state, but keep the control position gone invisible state, does not keep the position |
TextView
- Display processing of long text
- Support Html code
- The content has styles, link effects
Inheritance
View -> TextView -> Button
-> EditView
<?xml version="1.0" encoding="utf-8"?>
<!--ScrollView 滚动显示,但是只能放置一个子控件-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TextActivity"
android:orientation="vertical"
>
<!--
android:textSize 设置字体大小
android:textColor 设置字体颜色
android:lineSpacingMultiplier 倍距
android:lineSpacingExtra 精确行距
android:lines 设置显示行数
android:singleLine 只显示一行
android:ellipsize 省略号的位置 start middle end marquee 跑马灯
android:focusable="true" 设置可以获取焦点
android:focusableInTouchMode="true" 设置在触摸时可以获取焦点
android:marqueeRepeatLimit="marquee_forever" 跑马灯的重复次数
-->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/long_text"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
/>
</LinearLayout>
EditView
<!--
android:inputType 输入类型
textPassword 密码
number 只能是正整数
numberSigned 整数
numberDecimal 小数
numberDecimal | numberSigned 同时使用两种属性
android:hint 输入提示
android:textColorHint 输入提示的文字颜色
android:maxLength 输入文字的最大长度
-->
<EditText
android:layout_width="match_parent"
android:layout_height="68dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="25dp"
android:inputType="text"
android:hint="Name and Surname"
android:gravity="center"
android:textColorHint="#cccccc"
android:background="@mipmap/border"
android:maxLength="12"/>
Button
- Custom inner class
- Anonymous inner class
- The current Activity to implement the event interface
- Add click event attributes to the layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ButtonActivity"
android:orientation="vertical">
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="通过自定义内部类实现点击事件"/>
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="通过匿名内部类实现点击事件"/>
<Button
android:id="@+id/btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="通过当前Activity实现点击事件接口"/>
<Button
android:id="@+id/btn4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="在xml文件中绑定1"
android:onClick="myClick"/>
<Button
android:id="@+id/btn5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="在xml文件中绑定2"
android:onClick="myClick"/>
</LinearLayout>
package com.example.myapplication
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
class ButtonActivity : AppCompatActivity(), View.OnClickListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_button_acticity) // 应该先添加布局文件之后才能获取布局文件中定义的控件
//一、自定义内部类
//1.根据id获取按钮
var btn1 : Button = findViewById(R.id.btn1)
var mcl = MyClickListener()
btn1.setOnClickListener(mcl) // 3.为按钮注册点击事件监听器
//二、匿名内部类 只使用一次时适用
val btn2 = findViewById<Button>(R.id.btn2)
btn2.setOnClickListener{
Log.e("Tag", "匿名内部类监听器对象的按钮") }
//三、当前Activity去实现事件接口
val btn3 = findViewById<Button>(R.id.btn3)
btn3.setOnClickListener(this)
}
override fun onClick(v: View?) {
Log.e("Tag", "当前Activity去实现事件接口的按钮")
}
// 2. 创建内部类,继承onClickListener接口
internal inner class MyClickListener : View.OnClickListener {
override fun onClick(v: View?) {
Log.e("Tag", "内部类监听器对象的按钮")
}
}
//四、参数:被点击的控件对象
fun myClick(v: View?){
if (v != null) {
when(v.id){
R.id.btn4 -> Log.e("Tag", "在布局文件中添加点击事件属性1")
R.id.btn5 -> Log.e("Tag", "在布局文件中添加点击事件属性2")
}
}
}
}
ImageView
The controls used to display and control the image can be zoomed in, zoomed out, and rotated.
<!--
android:src 指定前景图片资源 会保持自身的比例不会变形
android:background 设置背景
-->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/add_photo"
android:layout_margin="10dp"/>
<!--
没有text属性
-->
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/add_photo"/>
ProgressBar
The progress bar, by default, is circular, without scale, just an animation effect that continuously rotates. By setting the style, the traditional horizontal scaled progress bar can be displayed.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ProgressBarConvert"
android:orientation="vertical">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<!--
style 风格
android:progress 设置进度
android:max 设置最大值
-->
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:progress="30"
android:max="200"/>
<!--
android:indeterminate 设置进度条一直滚动
-->
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:indeterminate="true"/>
<ProgressBar
android:id="@+id/progress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
/>
</LinearLayout>
package com.example.myapplication
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ProgressBar
class ProgressBarActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_progress_bar)
var progressBar = findViewById<ProgressBar>(R.id.progress)
progressBar.progress = 80
//在Android中,4.0之后是不能直接在线程中操作控件的
//进度条是一个特例
object : Thread() {
override fun run(){
for(i in 1..100){
progressBar.progress = i
try {
sleep(30)
} catch (e: InterruptedException) {
e.printStackTrace()
}
}
}
}
}
}
Empty
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="68dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="25dp"
android:inputType="text"
android:hint="Name and Surname"
android:gravity="center"
android:textColorHint="#cccccc"
android:background="@mipmap/border"/>
<EditText
android:id="@+id/pwd"
android:layout_width="match_parent"
android:layout_height="68dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="25dp"
android:inputType="textPassword"
android:hint="Password"
android:gravity="center"
android:textColorHint="#cccccc"
android:background="@mipmap/border"
android:maxLength="12"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:background="@mipmap/btn"
android:onClick="register"/>
fun register(v : View?){
val nameEdt = findViewById<EditText>(R.id.name)
val pwdEdit = findViewById<EditText>(R.id.pwd)
val progressBar = findViewById<ProgressBar>(R.id.pro_bar)
val name = nameEdt.text.toString()
val pwd = pwdEdit.text.toString()
if (name == "" || pwd == "") {
//无焦点提示
//参数1:环境上下文 参数2:提示性文本 参数3:提示持续时间
Toast.makeText(this, "姓名或密码不能为空", Toast.LENGTH_SHORT)
} else {
//都不为空,出现进度条
progressBar.visibility = View.VISIBLE
object : Thread() {
override fun run() {
for (i in 0..100) {
progressBar.progress = i
try {
sleep(30)
} catch (e: InterruptedException) {
e.printStackTrace()
}
}
}
}.start()
}
}