活动 eal enc panel .html http 中间 编写
和
- View
- ViewGrou
Android 应用中的所有用户界面元素都是使用
和
- View
对象构建而成。
- ViewGroup
对象用于在屏幕上绘制可供用户交互的内容。
- View
对象用于储存其他
- ViewGroup
(和
- View
)对象,以便定义界面的布局。
- ViewGroup
说明:
View 是安卓中最基本的一种 UI,它可以在屏幕上绘制一块矩形区域,并能响应这块区域的各种事件,我们使用的各种控件都是在 View 的基础上进行的添加和完善。ViewGroup 则是一种特殊的 View,他可以包含很多 View 和子 ViewGroup,是一个用于放置控件和布局的容器。
比如现在我们要替换系统默认的标题栏,改为如下所示:
即两边是两个按钮,中间是标题文字,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="@color/colorPrimary">
- <ImageButton
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:src="@drawable/previous_24px"
- />
- <TextView
- android:id="@+id/textView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_weight="1"
- android:gravity="center"
- android:text="我是标题文字"
- android:textColor="#fff"
- android:textSize="24sp" />
- <ImageButton
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/next_24px"
- />
- </LinearLayout>
我们面临一个问题,我们要让所有的活动页面都采用这个标题栏该怎么做呢?
我们可以单独将这个标题栏写到一个 XML 文件中,然后在其他任意使用此标题栏的活动的布局文件中引入此 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">
- <include layout="@layout/panel_title"/>
- </LinearLayout>
说明:
引入布局的技巧的确实解决了重复编写布局代码的问题,但是如果布局中的一些控件要求能够响应事件,我们还是需要在每个活动中为这些控件单独编写一次事件注册的代码,比如标题栏中的返回按钮,都是销毁当前活动。为了避免重复代码,我们可以使用自定义控件的方式来解决。
代码:
1. 新建 TitleLayout(继承自 LinearLayout)
- public classTitleLayoutextends LinearLayout {
- public TitleLayout(Context context, @Nullable AttributeSet attrs) {
- super(context, attrs);
- LayoutInflater.from(context).inflate(R.layout.panel_title,this);
- }
- }
说明:
首先我们重写了 LinerLayout 的构造函数,在布局中引入 TitleLayout 控件就会调用这个构造函数。然后在构造函数中对标题栏布局进行动态加载,这就需要用到 LayoutInflater 了。通过 LayoutInflater 的 form() 方法可以构建出一个 LayoutInflater 对象,然后调用 Inflate() 就可以动态加载一个布局文件。
inflate 接受两个参数:
2. 为按钮绑定事件
- public classTitleLayoutextends LinearLayout {
- public TitleLayout(Context context, @Nullable AttributeSet attrs) {
- super(context, attrs);
- LayoutInflater.from(context).inflate(R.layout.panel_title,this);
- ImageButton titleBack = (ImageButton) findViewById(R.id.title_back);
- titleBack.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- Toast.makeText(getContext(),"活动销毁",Toast.LENGTH_LONG).show();
- }
- });
- }
- }
3. 在布局文件中添加这个自定义控件
- <?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.zy.dealhelper.TitleLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- </LinearLayout>
说明:
添加自定义控件和添加普通控件的方式基本一样,只不过在添加自定义控件的时候需要指明控件的具体类名。
Android: 日常学习笔记 (7)———探究 UI 开发 (4)
来源: http://www.bubuko.com/infodetail-2079487.html