本文实例为大家分享了Android简单的自定义标题栏,供大家参考,具体内容如下
android自定义控件向来都是开发者最头疼的,但是我们要有那种迎难而上的精神。
MainActivity
package com.example.customview;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
/*
android自定义标题组合控件
步骤:
1.首先写出需要功能的布局 ,分析布局的父控件是谁?
例如水平布局 父控件应该是linearlayout较为合适
2.创建自定义控件类并继承 父控件
3.在构造方法中使用layoutInflat动态加载布局
*/
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();
}
}
}
Layout.class
package com.example.customview.custom;
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.TextView;
import android.widget.Toast;
import com.example.customview.R;
/**
* 自定义标题栏 并赋有点击事件
*/
public class Layout extends LinearLayout implements View. Listener {
private Button btback, btopen;
private TextView tv ;
public Layout(Context context, AttributeSet attrs) {
super(context, attrs);
//动态加载标题栏布局
LayoutInflater.from(context).inflate(R.layout.custom_layout, this);
initView();
}
private void initView() {//初始化控件
btback = (Button) findViewById(R.id.btback);
btback.set Listener(this);
btopen = (Button) findViewById(R.id.btopen);
btopen.set Listener(this);
tv = (TextView) findViewById(R.id.tv );
tv .set Listener(this);
}
@Override
public void (View view) {//监听点击事件
switch (view.getId()) {
case R.id.btback:
((Activity) getContext()).finish();
Toast.makeText(getContext(), \"销毁当前Activity\", Toast.LENGTH_SHORT).show();
break;
case R.id.btopen:
Toast.makeText(getContext(), \"展开\", Toast.LENGTH_SHORT).show();
break;
case R.id.tv :
Toast.makeText(getContext(), \"标题\", Toast.LENGTH_SHORT).show();
break;
}
}
}
activity_main.
<? version=\"1.0\" encoding=\"utf-8\"?> <LinearLayout ns:android=\"http://schemas.android.com/apk/res/android\" ns:tools=\"http://schemas.android.com/tools\" android:layout_width=\"match_parent\" android:layout_height=\"match_parent\" android:orientation=\"vertical\" tools:context=\"com.example.customview.MainActivity\"> <include layout=\"@layout/custom_layout\" /> <com.example.customview.custom. Layout android:layout_width=\"match_parent\" android:layout_height=\"wrap_content\" /> </LinearLayout>
custom_layout.
<? version=\"1.0\" encoding=\"utf-8\"?> <LinearLayout ns:android=\"http://schemas.android.com/apk/res/android\" ns:tools=\"http://schemas.android.com/tools\" android:layout_width=\"match_parent\" android:layout_height=\"match_parent\" android:orientation=\"vertical\" tools:context=\"com.example.customview.MainActivity\"> <include layout=\"@layout/custom_layout\" /> <com.example.customview.custom. Layout android:layout_width=\"match_parent\" android:layout_height=\"wrap_content\" /> </LinearLayout>
粘贴以上代码就可以运行了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
继续阅读与本文标签相同的文章
上一篇 :
扎根 AI 二十年,眼神科技周军解读智能安防发
-
DBengine 排名第一,云数据库 InfluxDB 版正式商业化!
2026-05-19栏目: 教程
-
阿里有个超神秘的组织,CEO们来了都要翻牌 | 开发者必读(056期)
2026-05-19栏目: 教程
-
云原生计算重塑企业IT架构 - 分布式应用架构
2026-05-19栏目: 教程
-
阿里云创新大会再出发,一些变化即将发生
2026-05-19栏目: 教程
-
阿里云的包年包月、按量付费、抢占式实例计费方式是什么,如何选择
2026-05-19栏目: 教程
