TextView
文本框
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="字号20pt,文本框结束处绘制图片"
android:textSize="20pt"
android:drawableEnd="@mipmap/ic_launcher"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="所有内容单行显示,中间的字进行省略,字母大写abc"
android:textSize="20sp"
android:ellipsize="middle"
android:textAllCaps="true"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="邮件是123@163.com,电话是12345678900"
android:autoLink="email|phone"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="颜色,阴影"
android:shadowColor="#00f"
android:shadowDx="10.0"
android:shadowDy="8.0"
android:shadowRadius="3.0"
android:textColor="#f00"
android:textSize="18pt"/>
<CheckedTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="可勾选的文本"
android:checkMark="@drawable/ic_baseline_check_24"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="边框"
android:textSize="24pt"
android:background="@drawable/ic_baseline_gamepad_24"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="圆角边框,渐变背景"
android:textSize="24pt"
android:background="@drawable/bg_border"/>
</LinearLayout>
bg_border.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:topLeftRadius="20dp"
android:bottomRightRadius="20dp"
android:bottomLeftRadius="10dp"/>
<stroke android:width="4px" android:color="#f0f"/>
<gradient android:startColor="#0f0"
android:endColor="#00f"
android:type="sweep"/>
</shape>
运行结果如下
动态布局
MainActivity.java
package com.example.toggle;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ToggleButton toggleButton=findViewById(R.id.toggle);
Switch aSwitch = findViewById(R.id.swicher);
LinearLayout test=findViewById(R.id.test);
//抽象的复合按钮
CompoundButton.OnCheckedChangeListener listener=((button, isChecked) -> {
if (isChecked){
//垂直布局
test.setOrientation(LinearLayout.VERTICAL);
toggleButton.setChecked(true);
aSwitch.setChecked(true);
}else {
//水平布局
test.setOrientation(LinearLayout.HORIZONTAL);
toggleButton.setChecked(false);
aSwitch.setChecked(false);
}
});
toggleButton.setOnCheckedChangeListener(listener);
aSwitch.setOnCheckedChangeListener(listener);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ToggleButton android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="横向"
android:textOn="纵向"
android:checked="true"/>
<Switch android:id="@+id/swicher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="横向"
android:textOn="纵向"
android:thumb="@drawable/ic_baseline_pets_24"
android:checked="true"/>
<LinearLayout android:id="@+id/test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button android:id="@+id/bn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1"/>
<Button android:id="@+id/bn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮2"/>
<Button android:id="@+id/bn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮3"/>
</LinearLayout>
</LinearLayout>
运行结果如下
时钟
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center_horizontal">
<!--模拟时钟-->
<AnalogClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<!--数字时钟-->
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#f0f"
android:format12Hour="yyyy年MM月dd日 H:mma EEEE"
android:drawableEnd="@mipmap/ic_launcher"/>
<!--模拟时钟-->
<AnalogClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:dial="@drawable/ic_baseline_radio_button_unchecked_24"
android:hand_minute="@drawable/ic_baseline_blur_off_24"/>
</LinearLayout>
运行结果如下
关联联系人
package com.example.ui08;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.QuickContactBadge;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取组件
QuickContactBadge quickContactBadge=findViewById(R.id.badge);
//建立关联
quickContactBadge.assignContactFromPhone("020-12345678",false);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<QuickContactBadge
android:id="@+id/badge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="通讯录"/>
</LinearLayout>
运行结果如下: