android 开发 使用自定义布局实现标题栏复用(标题栏内容自定义:使用代码实现和xml布局自定义属性2种办法实现)
在个人学习的情况下可能很少使用自定义布局去实现大量复用的情况下,但是在一个开发工作的环境下就会使用到大量复用的自定义控件。
实现思维:
1.写一个xml的布局,用于标题栏的样式,并且添加在标题栏中你想要的其他控件Button、TextView、Image
View 等等
2.单独写一个class去继承LinearLayout 或者 View 等等其他布局都行。(下面代码中我使用的是继承LinearLayout,其他布局可能需要复写的方法都有所不同)
3.将写好的xml布局到class中用LayoutInflater 布局膨胀器获得布局。
4.隐藏原来系统的标题栏(两种办法:代码实现和AndroidManifest中实现)
5.在其他布局中导入这个class的布局路径,实现标题栏的导入。
6.自定义标题栏内容,比如标题栏名称(两种办法:代码实现和添加自定义布局属性实现)
7.添加点击事件,在布局类中添加布局控件的点击事件,一次添加就可以不用后续复写需要重复使用的点击事件
1.写一个xml的布局,用于标题栏的样式,并且添加在标题栏中你想要的其他控件Button、TextView、Image
View 等等
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBlue">
<ImageView
android:id="@+id/title_left_Image"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:src="@mipmap/ic_left"
android:layout_gravity="center"/>
<TextView
android:id="@+id/title_text"
android:layout_width="0dp"
android:layout_weight="5"
android:layout_height="wrap_content"
android:text="标题"
android:textColor="@color/colorWhite"
android:textSize="@dimen/BigTextSize"
android:gravity="left"
android:layout_gravity="center"
android:layout_marginLeft="10dp"/>
<ImageView
android:id="@+id/title_add"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:src="@mipmap/ic_add"
android:layout_gravity="center"
android:visibility="gone"/>
</LinearLayout>
备注:在上面的xml中在id是title_add的这ImageView控件里,我写了隐藏此控件。说明一下为什么隐藏,因为在个别的activity里标题栏需要一些不同的控件出现,比如菜单栏图标或者加号图标,这些图标可以预先添加到布局中,在不使用的情况下隐藏它(android:visibility="gone" 其他:visible 显示 gone不显示且取消布局站位 invisible 不显示但是依然在布局上占据位置),在需要使用的时候在代码上或者自定义属性里显示这个控件。
效果图:
2.单独写一个class去继承LinearLayout 或者 View 等等其他布局都行。(下面代码中我使用的是继承LinearLayout,其他布局可能需要复写的方法都有所不同)
3.将写好的xml布局到class中用LayoutInflater 布局膨胀器获得布局。
package com.example.lenovo.mydemoapp.myLayout;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.lenovo.mydemoapp.R;
/**
* Created by lenovo on 2018/5/17.
*/
public class TitleLayout extends LinearLayout {
public TitleLayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.title_layout,this);
}
}
4.隐藏原来系统的标题栏(两种办法:代码实现和AndroidManifest中实现)
4.1首先是代码上实现隐藏:
若是继承Activity,则使用:
requestWindowFeature(Window.FEATURE_NO_TITLE);
public class Main2Activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/***
* 若是继承Activity,那么此时requestWindowFeature(Window.FEATURE_NO_TITLE);有效;
* 此时的Activity是不支持getSupportActionBar().hide()这个方法的;
* **/
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main2);
}
}
若是继承AppCompatActivity,则只需加入一条语句:
getSupportActionBar().hide();
示例如下:
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
if (actionBar!=null){
//隐藏标题栏
actionBar.hide();
}
}
}
4.2
在布局文件中进行设置
整个应用都不显示标题栏:
如果是想让标题栏在整个应用中都不显示,那么,则可在AndroidManifest.xml中的<application>节点上
设定其属性android:theme为带有NoActionBar的值,这样所创建的所有activity都不会带有标题栏了;如:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lenovo.mydemoapp">
<!-- 此处添加静态权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<!--隐藏标题栏 android:theme="@style/Theme.AppCompat.Light.NoActionBar"-->
<application
android:name=".myAppCompatActivity.MyApplication"
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/Theme.AppCompat.Light.NoActionBar">
注:由于<application>节点的android:theme指定为了@style/Theme.AppCompat.Light.NoActionBar,
与此同时,又由于其所有的子节点<activity>的属性android:theme都并未指定,这样所有的activity就都不会带有标题栏
4.3
某一个activity不显示标题栏:
若是想让应用中的某一个activity不显示标题栏,则可设定对应的activity的属性android:theme为带有NoActionBar的值,如:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Main2Activity"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
></activity>
</application>
5.解决了标题栏隐藏的问题,现在我们在其他布局中导入这个class的布局路径,实现自定义标题栏的使用。
<?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">
<com.example.lenovo.mydemoapp.myLayout.TitleLayout
android:id="@+id/ManualBinding_title"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.example.lenovo.mydemoapp.myLayout.TitleLayout>
导入很简单只需要在布局里直接写全路径就行,注意以下标题栏的位置就行。
6.自定义标题栏内容,比如标题栏名称(两种办法:代码实现和添加自定义布局属性实现)
6.1 代码上实现修改标题栏中指定控件的内容修改
在代码上实现修改标题栏内容,一定要给我们我们添加布局中的标题栏布局一个id,我们需要这个id去定位想要修改的标题栏是那一个
<com.example.lenovo.mydemoapp.myLayout.TitleLayout
android:id="@+id/ManualBinding_title"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.example.lenovo.mydemoapp.myLayout.TitleLayout>
然后是代码上:
public class ManualBinding extends AppCompatActivity {
private TitleLayout mTitleLayout;
private TextView mTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manual_binding);
mTitleLayout = (TitleLayout)findViewById(R.id.ManualBinding_title);
mTitle = (TextView)mTitleLayout.findViewById(R.id.title_text);
mTitle.setText("手动绑定");
}
}
可以看到很简单,找到布局里的标题栏控件,在用标题栏控件找到要修改的TextView,当然代码上实现不单单只有这些,还可以修改ImageView 等等其他控件的属性参数,这个请自行查找更详细的代码修改布局的方式。
6.2 用自定义布局属性的方法实现标题栏自定义内容的修改:
6.2.1 第一步我们先要实现自定义属性的注册,进入values项目创建一个attrs.xml文件
<resources>
<!-- Declare custom theme attributes that allow changing which styles are
used for button bars depending on the API level.
?android:attr/buttonBarStyle is new as of API 11 so this is
necessary to support previous API levels. -->
<declare-styleable name="ButtonBarContainerTheme">
<attr name="metaButtonBarStyle" format="reference" />
<attr name="metaButtonBarButtonStyle" format="reference" />
</declare-styleable>
<!--注意下面的name写的TitleLayout 类名,另外注意添加属性之间的空格-->
<declare-styleable name="TitleLayout">
<!--字符串-->
<attr name="titleNameText" format="string"/>
<!--颜色-->
<attr name="a" format="color"/>
<!--尺寸-->
<attr name="b" format="dimension"/>
<!--枚举-->
<attr name="c" format="enum"/>
<!--常量或者变量值-->
<attr name="d" format="flag"/>
<!--分数-->
<attr name="e" format="fraction"/>
<!--整数int值-->
<attr name="f" format="integer"/>
<!--参数值-->
<attr name="g" format="reference"/>
<!--浮点小数-->
<attr name="h" format="float"/>
<!--布尔值-->
<attr name="i" format="boolean"/>
</declare-styleable>
</resources>
请关注<declare-styleable name="TitleLayout"> 的内容,首先要注意name="TitleLayout" 的名称要与我们布局class的名称一致。
然后就是添加自定义布局的属性,属性名称 与 值的属性,我现在只需要一个string的自定义布局属性,不过为了节约看官们的时间,我标准了其他值的属性。大家可以参考参考
6.2.2 以上添加完成后,我们要回到布局的class中,去添加自定义属性的调用
public class TitleLayout extends LinearLayout {
private ImageView mTitleLeftImage,mTitle;
private TextView mTitleText;
private String mTitleName = "标题";
public TitleLayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.title_layout,this);
mTitleText = (TextView)findViewById(R.id.title_text);
/*
添加自定义标题
*/
//TypedArray键入数组 obtainStyledAttributes获取样式属性 得到attrs.xml里的name名称
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.TitleLayout);
//从对应name名称的数组下面得到自定义属性
mTitleName = typedArray.getText(R.styleable.TitleLayout_titleNameText).toString();
//将自定义属性与布局匹配
mTitleText.setText(mTitleName);
}
}
6.2.2 最后是标题栏布局里添加需要的属性参数
PS:这里需要注意的一点!在父类布局中一定要添加xmlns:app="http://schemas.android.com/apk/res-auto" 这条属性 我们才能使用自定义属性前关键字app:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.lenovo.mydemoapp.AboutUs">
<com.example.lenovo.mydemoapp.myLayout.TitleLayout
app:titleNameText="关于我们"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.example.lenovo.mydemoapp.myLayout.TitleLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"></LinearLayout>
</LinearLayout>
运行效果:
7.添加点击事件,在布局类中添加布局控件的点击事件,一次添加就可以不用后续复写需要重复使用的点击事件
package com.example.lenovo.mydemoapp.myLayout;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.lenovo.mydemoapp.R;
/**
* Created by lenovo on 2018/5/17.
*/
public class TitleLayout extends LinearLayout {
private ImageView mTitleLeftImage,mTitle;
private TextView mTitleText;
private String mTitleName = "标题";
public TitleLayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.title_layout,this);
mTitleText = (TextView)findViewById(R.id.title_text);
/*
添加自定义标题
*/
//TypedArray键入数组 obtainStyledAttributes获取样式属性 得到attrs.xml里的name名称
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.TitleLayout);
//从对应name名称的数组下面得到自定义属性
mTitleName = typedArray.getText(R.styleable.TitleLayout_titleNameText).toString();
//将自定义属性与布局匹配
mTitleText.setText(mTitleName);
/*
添加返回键点击事件
*/
mTitleLeftImage = (ImageView)findViewById(R.id.title_left_Image);
mTitleLeftImage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
((Activity)getContext()).finish();
}
});
}
}
android 开发 使用自定义布局实现标题栏复用(标题栏内容自定义:使用代码实现和xml布局自定义属性2种办法实现)的更多相关文章
- Android开发3:Intent、Bundle的使用和ListView的应用 、RelativeLayout(相对布局)简述(简单通讯录的实现)
前言 啦啦啦~博主又来骚扰大家啦~大家是不是感觉上次的Android开发博文有点长呢~主要是因为博主也是小白,在做实验的过程中查询了很多很多概念,努力去理解每一个知识点,才完成了最终的实验.还有就是随 ...
- Android开发教程 - 使用Data Binding(八)使用自定义Interface
本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...
- Android开发如何在4.0及以上系统中自定义TitleBar
本文将通过一个实例讲解怎么实现在4.0及以上系统版本中实现自定义TitleBar,这只是我自己找到的一种方法; xml布局文件 activity_main.xml <RelativeLayout ...
- Android开发问题集锦-Button初始为disable状态时自定义的selector不生效问题
1.下面是不生效的布局: selector_btn_red.xml: <?xml version="1.0" encoding="utf-8"?> ...
- 仿酷狗音乐播放器开发日志二十四 选项设置窗体的实现(附328行xml布局源码)
转载请说明原出处,谢谢~~ 花了两天时间把仿酷狗的选项设置窗体做出来了,当然了只是做了外观.现在开学了,写代码的时间减少,所以整个仿酷狗的工程开发速度减慢了.今天把仿酷狗的选项设置窗体的布局代码分享出 ...
- android 开发 ScrollView 控件的一些api描述与自定义ScrollView接口回调方法
1.正常使用ScrollView控件的一些api详解. package com.example.lenovo.mydemoapp.scrollViewDemo; import android.supp ...
- Android开发之创建App Widget和更新Widget内容
App WidgetsApp Widgets are miniature application views that can be embedded in other applications (s ...
- Android开发(30)--AutoCompleteTextView和MultiAutoCompleteTextView自动提示输入内容
首先大家都见过类似这种效果, AutoCompleteTextView是实现动态匹配输入的内容 下面就通过一个实例来说明AutoCompleteTextView,同样,AutoCompleteText ...
- Android开发1——查找所需要出示权限的内容
一.发现问题 用户在执行一些如拨打电话.发送短信等关系用户隐私的功能时,Android需要出示权限,权限在AndroidManifest.xml中配置 拨打电话的权限 发送短信的权限 那么这些权限信息 ...
随机推荐
- jira发送邮件报错
jira发送邮件的报错 1.安装完jira后,配置发送邮件出错具体报错如下: An error has occurred with sending the test email: com.atlass ...
- 系统服务和普通FORMS程序共存一体的实现
要求:一个EXE,如何将它做成这样的效果:1.双击它时,像一个FORMS程序那样正常显示窗体运行.2.注册成系统服务,每次都可以从service.msc中启动它. 也就是说,没注册之前,它可以当作普通 ...
- InfluxDB HTTP API reference
InfluxDB HTTP API reference API地址:https://docs.influxdata.com/influxdb/v1.6/tools/api/ The InfluxDB ...
- 【maven】之打包war依赖子项目jar
比如 p-common p-core p-dao p-service p-web service项目依赖dao,dao依赖core和common,web依赖service 在使用maven tomca ...
- Netty Tutorial Part 1: Introduction to Netty [z]
Netty Tutorial, Part 1: Introduction to Netty Update: Part 1.5 Has Been Published: Netty Tutorial P ...
- vc++获取网页源码
1. 获取网页源码的步骤: com组件的初始化 创建WinHttpRequest对象 创建并实例化WinHttpRequest组件 调用Open方法打开连接 调用Send方法发送请求 使用Respon ...
- docker下centos安装ping命令
https://blog.csdn.net/king_gun/article/details/78423115 [问题] 从docker hub上拉取到则镜像centos:6.7在执行ping命令是报 ...
- Spring的LoadTimeWeaver(代码织入)(转)
https://www.cnblogs.com/wade-luffy/p/6073702.html 在Java 语言中,从织入切面的方式上来看,存在三种织入方式:编译期织入.类加载期织入和运行期织入. ...
- ospf精确宣告地址
ospf的一点小问题 http://bbs.51cto.com/thread-881459-1.html 参照博客地址 network 172.20.1.0 0.0.0.3 area 0 networ ...
- WireGuard协议介绍
原文: https://www.jianshu.com/p/32c3e62c2711