最近利用下班时间,找了看什么书比较适合初学android的朋友,很多人推荐了这本书,于是就买了一本,感觉看书,思考,动手,再思考和总结这样过程还是很有必要的,于是就打算把自己学习的东西简单的总结一下;方便自己以后查找,也有利于学习的巩固。在这里首先要感谢一下书籍的作者——郭霖前辈。
前一篇介绍了四大布局,但是我们在编写布局时,很多时候会使用到相同的布局或者局部相同,比如我每次都喜欢在主界面使用一个垂直线性布局,然后添加几个按钮实现展示的效果;虽然这是一个不恰当的例子,但是在几篇之后我们就可以通过一个自定义布局(自定义控件)结合ListView来实现相同的效果,下次使用的时候,直接修改ListView中自定义布局数目以及自定义布局中的按钮点击事件即可。自定义控件和自定义布局之所以放在一块,主要是因为自定义控件时依托于自定义布局的,也就是自定义控件在自定义布局基础上对其内部的部分基本控件赋予可执行逻辑业务的功能。
1,布局引入和自定义控件
主要的作用就是简化编码,把经常使用的布局内容提取出来封装到一个布局中,通过include直接引入该布局名称到其他布局中即可完成加载该布局的目的,所以使用非常的简单。自定义控件正如上面所说,主要用于赋予自定义布局中基本控件处理业务逻辑的能力;通常是自定义一个布局类继承自定义布局类的类型,然后重写构造方法,在里面添加上赋予控件的一些功能或者修改控件属性。
2,示例代码
MainActivity.java代码:
package com.hfut.operatinquotelayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar=getSupportActionBar();
if(actionBar!=null){
actionBar.hide();
}
}
}
activity_main.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.hfut.operatinquotelayout.MainActivity">
<include layout="@layout/selflayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</include>
<TextView
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:textSize="20dp"
android:text="下面是通过引入自定义布局的方式添加自定义布局"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<include layout="@layout/listitemlayout"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</include>
<TextView
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:textSize="20dp"
android:text="下面是通过引入自定义布局控件的方式添加自定义布局"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<com.hfut.operatinquotelayout.ListItemLayout
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.hfut.operatinquotelayout.ListItemLayout>
<!--<include layout="@layout/selflayout"-->
<!--></include>-->
<!--<include layout="@layout/listitemlayout"-->
<!--android:layout_marginTop="30dp"-->
<!--></include>-->
<!--<include layout="@layout/selflayout"-->
<!--></include>-->
<!--<include layout="@layout/listitemlayout"-->
<!--></include>-->
</LinearLayout>
selflayout.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="com.hfut.operatinquotelayout.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center_horizontal"
android:text="下面是垂直的线性布局"
android:textSize="20dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:textAllCaps="false" />
<Button
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2"
android:textAllCaps="false" />
<Button
android:layout_gravity="end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3"
android:textAllCaps="false" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:text="下面是水平的线性布局"
android:textSize="20dp" />
<LinearLayout
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button4"
android:textAllCaps="false" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button5"
android:textAllCaps="false" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button6"
android:textAllCaps="false" />
</LinearLayout>
</LinearLayout>
listitemlayout.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_alignParentRight="true"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@mipmap/ic_launcher"
android:id="@+id/ItemImage"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下载更新"
android:id="@+id/ItemBottom"
android:focusable="false"
android:layout_toLeftOf="@+id/ItemImage" />
<TextView android:id="@+id/ItemTitle"
android:layout_height="wrap_content"
android:text="标题"
android:layout_width="fill_parent"
android:textSize="20sp"/>
<TextView android:id="@+id/ItemText"
android:text="文本内容"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="@+id/ItemTitle"/>
</RelativeLayout>
ListItemLayout.java代码:
package com.hfut.operatinquotelayout;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
* author:why
* created on: 2018/3/28 11:38
* description:
*/
public class ListItemLayout extends RelativeLayout {
ImageView icon;
TextView title;
TextView content;
Button update;
public ListItemLayout(Context context, AttributeSet attrs) {
super(context, attrs);
//加载布局到该自定义布局控件
LayoutInflater.from(context).inflate(R.layout.listitemlayout,this);
initUI();
update.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
update.setText("更新中");
content.setText("正在下载...");
}
});
}
private void initUI() {
icon=findViewById(R.id.ItemImage);
title=findViewById(R.id.ItemTitle);
content=findViewById(R.id.ItemText);
update=findViewById(R.id.ItemBottom);
}
}
主配置文件AndroidManifest.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hfut.operatinquotelayout">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
3,运行结果
第一步:运行程序

第二步:点击“引入布局下载更新”按钮

第三步:点击“自定义控件下载更新”按钮

其中selflayout中代码是直接使用四大布局总结中的部分代码,主要用于展示引入布局的使用;listitemlayout和ListItemLayout主要用于对比引入自定义布局和自定义控件的区别。
总结:这部分内容主要为后续常用的自定义控件学习做一些铺垫,所以学起来还是比较简单的。
注:欢迎扫码关注


923

被折叠的 条评论
为什么被折叠?



