Android
定义字符串、颜色、图片
-
其他地方可以引用
-
src\main\res\values\strings.xml
<resources>
<string name="app_name">myapp</string>
<string name="textViewText">hello</string>
</resources>
- src\main\res\values\colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="red">#FFFF0000</color>
</resources>
- src\main\res\drawable\btn_selector.xml
- 背景图片选择器
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_baseline_accessibility_24" android:state_pressed="true"/>
<item android:drawable="@drawable/ic_baseline_account_circle_24" />
</selector>
- src\main\res\color\btn_color_selector.xml
- 颜色选择器
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/red" android:state_pressed="true"/>
<item android:color="@color/white" />
</selector>
- src\main\res\drawable、bg_username.xml
- 定义背景样式
- strock
- 框定义
- corners
- 形状
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1dp"
android:color="@color/white"
/>
<corners
android:radius="10dp"
/>
</shape>
控件
LinearLayout
- 线性布局
- android:layout_height=“match_parent”
- 高度与父控件一致
- android:layout_width=“match_parent”
- 宽度与父控件一致
- android:orientation=“vertical”
- vertical
- 内部控件垂直排列
- horizontal
- 内部控件水平排列
- vertical
- android:padding=“10dp”
- 相对四周10pd空格
- android:background=“@drawable/ic_launcher_background”
- 背景
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:background="@drawable/ic_launcher_background"
android:gravity="center">
<LinearLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@color/black"/>
<LinearLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@color/red"/>
<LinearLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@color/black"/>
</LinearLayout>
RelativeLayout
- 相对布局
- 需要指明相对位置
- android:layout_centerInParent=“true”
- android:layout_toRightOf=“@id/r1”
- android:layout_toLeftOf=“@id/r1”
- android:layout_below=“@id/r1”
- 需要指明相对位置
- android:id=“@+id/r1”
- 添加id
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<RelativeLayout
android:id="@+id/r1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@color/black"
android:layout_centerInParent="true"/>
<RelativeLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@color/red"
android:layout_toLeftOf="@id/r1"/>
<RelativeLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@color/black"
android:layout_toRightOf="@id/r1"/>
</RelativeLayout>
TextView
- android:text=“@string/textViewText”
- 显示的文本
- android:textColor=“@color/black”
- 文本颜色
- android:textSize=“20sp”
- 文字大小,单位sp
- android:maxEms=“10”
- 超过10自动换行
- android:maxLines=“1”
- 最多1行
- android:ellipsize=“end”
- 超过限制以以"…"结尾
- android:gravity=“center”
- 文字相对TextView布局
- android:layout_gravity=“center”
- TextView相对父控件布局
<TextView
android:id="@+id/textView"
android:layout_width="200dp"
android:layout_height="50dp"
android:text="@string/textViewText"
android:textStyle="bold"
android:textColor="@color/black"
android:textSize="20sp"
android:background="@color/red"
android:gravity="center"
android:maxEms="10"
android:maxLines="1"
android:ellipsize="end"
tools:ignore="MissingConstraints" />
- java
//根据id获取textView
TextView myTextView = findViewById(R.id.mtTextView);
EditText
- android:hint=“用户名”
- 显示文字
- android:maxLines=“1”
- 最多1行
- android:inputType=“textPassword”
- 输入类型
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textColor="@color/black"
android:hint="用户名"
android:maxLines="1"
android:padding="5dp"
android:textSize="16sp"
android:layout_marginTop="10dp"/>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textColor="@color/black"
android:hint="密码"
android:maxLines="1"
android:padding="5dp"
android:textSize="16sp"
android:inputType="textPassword"
android:layout_marginTop="10dp"/>
EditText editText = findViewById(R.id.editText2);
editText.getText().toString();
Button
- extend TextView
<Button
android:id="@+id/login"
android:layout_width="200dp"
android:layout_height="50dp"
android:text="登录"
android:textColor="@color/white"
android:textSize="20sp"
android:background="@color/white"
android:layout_gravity="center"
android:layout_marginTop="20dp"
/>
- 权重分配大小
- android:layout_width=“0dp”
- 必须是0dp
- android:layout_weight=“1”
- android:layout_width=“0dp”
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<Button
android:id="@+id/login"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="登录"
android:textColor="@color/white"
android:textSize="20sp"
android:background="@drawable/bg_username"
android:layout_gravity="center"
android:layout_margin="20dp"
/>
<Button
android:id="@+id/register"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="注册"
android:textColor="@color/white"
android:textSize="20sp"
android:background="@drawable/bg_username"
android:layout_gravity="center"
android:layout_margin="20dp"
/>
</LinearLayout>
ImageView
<ImageView
android:id="@+id/imageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:maxHeight="200dp"
android:maxWidth="200dp"
android:src="@drawable/ic_baseline_account_circle_24"
tools:ignore="MissingConstraints" />
ProgressBar
- 显示与隐藏
- progressBar.getVisiblity()
- progressBar.setVisibility
- View.VISIBLE
- View.GONE
- 进度条
- progressBar.setProgress()
- int
- progressBar.setProgress()
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="224dp"
tools:layout_editor_absoluteY="151dp"
tools:ignore="MissingConstraints" />
Notification
-
通知公告
-
定义
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
NotificationChannel notificationChannel = new NotificationChannel("notification", "测试通知", NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(notificationChannel);
}
Notification notification = new NotificationCompat.Builder(this,"notification")
.setContentTitle("访客通知")
.setContentText("测试")
.setSmallIcon(R.drawable.ic_baseline_accessibility_24)
.build();
- 发送
notificationManager.notify(1,notification)
- 取消
notificationManager.cancel(1)
ToolBar
<androidx.appcompat.widget.Toolbar
android:background="#ffff00"
app:navigationIcon="@drawable/ic_baseline_account_circle_24"
app:title="标题"
app:titleTextColor="@color/red"
app:titleMarginStart="90dp"
app:subtitle="子标题"
app:subtitleTextColor="@color/black"
app:logo="@mipmap/ic_launcher"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
tools:ignore="MissingConstraints" />
AlertDialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher)
.setTitle("弹出框")
.setMessage("xxxx")
.create()
.show();
PopupWindow
View popupView = getLayoutInflater().inflate(R.layout.popup_window,null);
PopupWindow popupWindow = new PopupWindow(popupView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
true);
popupWindow.showAsDropDown(xxx);
FrameLayout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<FrameLayout
android:id="@+id/r1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@color/black"
android:foreground="@color/red">
<FrameLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@color/red"
android:foregroundGravity="center"/>
</FrameLayout>
TableLayout
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<TableRow>
<Button>
</Button>
</TableRow>
<TableRow>
<Button>
</Button>
</TableRow>
</TableLayout>
GridLayout
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<Button>
</Button>
</GridLayout>
ConstraintLayout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/mtTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
</androidx.constraintlayout.widget.ConstraintLayout>
ListView
Recylerview
ScrollView
- 只能包含一个控件
- 可以用LinearLayout
RadioGroup
- RadioGroup
- RadioButton
CheckBox
事件
- 跳转
// 声明控件
private Button login;
private Button register;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找到控件
login = findViewById(R.id.login);
register.findViewById(R.id.register);
// 跳转方法
login.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// 设置意图
Intent intent = new Intent(MainActivity.this,FunctionActivity.class);
startActivity(intent);
}
});
}
- 改进
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
// 声明控件
private Button login;
private EditText username;
private EditText password;
// private Button register;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找到控件
login = findViewById(R.id.login);
username = findViewById(R.id.username);
password = findViewById(R.id.password);
login.setOnClickListener(this);
}
public void onClick(View v){
// 设置意图
Intent intent = null;
// 获取输入的用户名和密码
String name = username.getText().toString();
String pass = password.getText().toString();
String ok = "登录成功";
String fail = "登录失败";
if(name.equals("wx")&&pass.equals("1")){
// toast 普通版
// Toast.makeText(getApplicationContext(),ok,Toast.LENGTH_SHORT).show();
ToastUtil.showMsg(getApplicationContext(),ok);
// 跳转
intent = new Intent(MainActivity.this,FunctionActivity.class);
startActivity(intent);
}else{
// 不正确
// toast提升版
// Toast toast = Toast.makeText(getApplicationContext(),fail,Toast.LENGTH_SHORT);
ToastUtil.showMsg(getApplicationContext(),fail);
// toast.setGravity(Gravity.CENTER,0,0);
// toast.show();
}
}
@Override
public void onPointerCaptureChanged(boolean hasCapture) {
}
}
public class ToastUtil {
private static Toast toast;
public static void showMsg(Context context, String msg){
if(toast==null){
toast = Toast.makeText(context,msg,Toast.LENGTH_SHORT);
}else{
toast.setText(msg);
}
toast.show();
}
}
- 改进
private void setListener(){
OnClick onClick = new OnClick();
// 对每一个按钮setClick
playButton.setOnClickListener(onClick);
studyButton.setOnClickListener(onClick);
}
private class OnClick implements View.OnClickListener{
@Override
public void onClick(View v) {
Intent intent = null;
switch (v.getId()){
case R.id.studyButton:intent = new Intent(SlideActivity.this,StudyActivity.class);break;
case R.id.playButton:intent = new Intent(SlideActivity.this,PlayActivity.class);break;
}
startActivity(intent);
}
}
- 其他
Button btn = findViewById(R.id.button);
//点击事件
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
//长按事件
btn.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return false;
}
});
//触摸事件
btn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
动画
- 补帧
- 补间
- 属性