3.1常用控件
3.1.1 TextView
3.1.2 Button
3.1.3 EditText
3.1.4 ImageView
3.1.5 ProgressBar
3.1.6 AlertDialog&ProgressDialog
3.2 四种基本布局
3.2.1 线性布局
- 通过android:orientation属性指定了排列方向是vertical,垂直排列
- 如果指定的是horizontal,控件就会在水平方向上排列了
3.2.2 相对布局
3.2.3 帧布局
3.2.4 百分比布局
3.3 自定义控件
3.3.1 引入布局
- 引入布局的技巧确实解决了重复编写布局代码的问题
创建一个新的xml,然后引入
3.3.2 自定义控件
- 但是如果布局中有一些控件要求能够响应事件,我们还是需要在每个活动中为这些控件单独编写一次事件注册的代码。比如说标题栏中的返回按钮,其实不管是在哪一个活动中,这个按钮的功能都是相同的,即销毁当前活动。而如果在每一个活动中都需要重新注册一遍返回按钮的点击事件,
无疑会增加很多重复代码,这种情况最好是使用自定义控件的方式来解决。
- 先写一个title.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="wrap_content"
android:background="#0f1423">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/title_back"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="#1c2938"
android:text="Back"
android:textColor="#fff" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/title_text"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="Title Text"
android:textColor="#fff"
android:textSize="24sp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/title_edit"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="#1c2938"
android:text="Edit"
android:textColor="#fff" />
</LinearLayout>
- 新建一个TitleLayout继承LinearLayout,首先我们重写了LinearLayout中带有两个参数的构造函数,在布局中引入TitleLayout控件就会调用这个构造函数。然后在构造函数中需要对标题栏布局进行动态加载,这就要借助LayoutInflater来实现了。通过LayoutInflater的from()方法可以构建出一个LayoutInflater对象,然后调用inflate()方法就可以动态加载一个布局文件,inflate()方法接收两个参数,第一个参数是要加载的布局文件的id,这里我们传入R.layout.title,第二个参数是给加载好的布局再添加一个父布局,这里我们想要指定为TitleLayout,于是直接传入this。
package com.example.uiwidgettest;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
public class TitleLayout extends LinearLayout {
public TitleLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.title, this);
Button titleBack = (Button) findViewById(R.id.title_back);
Button titleEdit = (Button) findViewById(R.id.title_edit);
titleBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
((Activity) getContext()).finish();
}
});
titleEdit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "You clicked Edit button",
Toast.LENGTH_SHORT).show();
}
});
}
}
- 修改MainActivity.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">
<com.example.uiwidgettest.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
3.4 ListView
3.4.1 创建简单ListView
3.4.2 定制化ListView
3.4.3 提升ListView效率
3.5 RecyclerView
3.5.1 滚动控件的基本用法
3.5.2 横向滚动和瀑布流布局
3.5.3 点击事件注册
3.6 实践
3.6.1 制作Nine-Patch图片
3.6.2 编写界面