
标题Toolbar控件


// 通过控件id查找布局中定义的Toolbar,赋值给toolbar对象
Toolbar toolbar = findViewById(R.id.tb);
// 设置Toolbar左上角返回箭头图标的点击监听器
toolbar.setNavigationOnClickListener(
// 创建点击事件监听器匿名内部类
new View.OnClickListener(){
// 重写点击触发的回调方法,参数v代表被点击的视图(这里是导航按钮)
@Override
public void onClick(View v){
// 打印错误类型日志,第一个参数TAG用来筛选日志,第二个是打印文本
Log.e(TAG,"onClick:toolbar被点击了");
}
}
);
<androidx.appcompat.widget.Toolbar
android:id="@+id/tb"
app:title="标题"
app:subtitle="子标题"
app:navigationIcon="@color/cardview_shadow_start_color"
app:logo="@drawable/baseline_sentiment_very_satisfied_24"
app:titleMarginStart="90dp"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#ffff00"
/>
AlertDialog提示框

// 加载自定义弹窗布局cs.xml,第二个参数null代表不绑定父容器
View bj = getLayoutInflater().inflate(R.layout.cs,null);
// 创建AlertDialog弹窗构建器,传入当前Activity上下文
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// 链式调用配置弹窗各项属性:图标、标题、提示文字、自定义布局、按钮,最后创建并展示弹窗
builder
// 设置弹窗左上角图标,使用mipmap下的应用默认图标
.setIcon(R.mipmap.ic_launcher)
// 设置弹窗顶部标题文字
.setTitle("love-gl烨")
// 设置弹窗主体默认提示文本(会被自定义布局bj覆盖,不显示)
.setMessage("教我钢琴")
// 给弹窗嵌入我们加载好的自定义布局cs.xml
.setView(bj)
// 设置右侧【喜欢】确认按钮
.setPositiveButton("喜欢", new DialogInterface.OnClickListener() {
// 按钮点击回调方法
@Override
public void onClick(DialogInterface dialog, int which) {
// 打印Error级日志,标记用户点击了确定按钮
Log.e(TAG,"确定");
}
})
// 设置左侧【取消】按钮
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.e(TAG,"取消");
}
})
// 设置中间中立按钮【左边】
.setNeutralButton("左边", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.e(TAG,"左边");
}
})
// 根据上面所有配置,生成弹窗对象
.create()
// 弹出并显示弹窗
.show();
<?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="20dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="horizontal"
android:gravity="center">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/baseline_sentiment_very_satisfied_24"/>
<TextView
android:text="@string/app_name"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

PopupWindow

public void leoClick(View view){
// 1. 加载弹窗自定义布局s_b.xml,第二个参数null表示不绑定父容器,PopupWindow加载固定传null
View s_b = getLayoutInflater().inflate(R.layout.s_b,null);
// 2. 创建PopupWindow弹窗对象
// 参数1:弹窗要显示的自定义布局视图s_b
// 参数2:弹窗宽度自适应内部内容 WRAP_CONTENT
// 参数3:弹窗高度固定1000像素
// 参数4:true 允许弹窗获取焦点(可响应返回键、外部点击关闭等事件)
PopupWindow popupWindow = new PopupWindow(s_b, ViewGroup.LayoutParams.WRAP_CONTENT
,1000,true);
// 3. 在触发点击的按钮view的下方,下拉弹出该弹窗
popupWindow.showAsDropDown(view);
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0"
>
<Button
android:textSize="41sp"
android:layout_width="match_parent"
android:layout_height="300dp"
android:text="测试" />
</LinearLayout>
<?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/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/id1"
android:text="ID1"
android:onClick="leoClick"
android:textSize="26sp"
android:layout_width="200dp"
android:layout_height="100dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
布局设置


布局:
FromeLayout 覆盖布局
TableLayout表格布局
- 在

GridLayout网格布局(只可设置列)

