简单的自定义标题栏
来源:互联网 发布:语玩刷金币软件下载 编辑:程序博客网 时间:2024/06/07 20:56
之前写项目时框架是别人搭的,标题栏是一个布局文件做了简单的封装
每次都要在新建的activity的xml布局文件中include
感觉有点麻烦
于是在原来项目的基础上又做了简单的处理
写了一个baseActivity
baseActivity的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" android:orientation="vertical"> <include layout="@layout/title_template"/> <FrameLayout android:id="@+id/view_mainBody" android:layout_width="match_parent" android:layout_height="match_parent"> </FrameLayout></LinearLayout>
包括一个标题栏 和一个内容区
标题栏 简单地写了3个textview和2个imageButton如下
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/titleBar" android:layout_width="match_parent" android:layout_height="48dp" android:background="@color/colorPrimaryDark"> <TextView android:id="@+id/title_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textColor="@color/white" android:textSize="18sp" android:text="我是标题"/> <TextView android:id="@+id/title_right_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="16dp" android:textColor="@color/white" android:textSize="18sp" android:text="右侧"/> <TextView android:id="@+id/title_left_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginLeft="16dp" android:textColor="@color/white" android:textSize="18sp" android:text="左侧"/> <ImageButton android:id="@+id/title_image_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:layout_marginRight="16dp" android:visibility="gone" /> <ImageButton android:id="@+id/title_image_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_alignParentLeft="true" android:layout_marginLeft="16dp" android:visibility="gone" /></RelativeLayout>BaseActivity代码如下public class BaseActivity extends FragmentActivity { // 主体显示 protected ViewGroup mainBody; // 是否使用模板 true 使用模板,false 不使用模板 protected boolean isTemplate = true; protected Title titleBar; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.template); mainBody = (ViewGroup) findViewById(R.id.view_mainBody); titleBar = new Title((RelativeLayout) findViewById(R.id.titleBar)); if (!isTemplate) { titleBar.hide(); } } @Override public void setContentView(int layoutResID) { if (layoutResID == R.layout.template) { super.setContentView(layoutResID); } else { mainBody.removeAllViews(); mainBody.addView(this.getLayoutInflater().inflate(layoutResID, null)); } } @Override public void setContentView(View view) { mainBody.removeAllViews(); mainBody.addView(view); } @Override public void setContentView(View view, ViewGroup.LayoutParams params) { mainBody.removeAllViews(); mainBody.addView(view, params); }}这样在我们需要使用带有标题栏的 activity时 只要继承我们的这个baseActivity就可以了@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); titleBar.setTitleType(Title.TYPE6) .setTitle("商城") .setBackgroundResource(R.color.black) .AddEventListener(Title.TITLE_LEFT_TEXT, new ICallBack() { @Override public void CallBackFunction(Object e) { finish(); } });}其中主要的对于标题栏的处理 在title这个类中,详情可以看下源码,封装的不是很好,但也可以起到一个参考作用吧。本人QQ:3553917035 有不明白的可以 一起聊聊
0 0
- 简单的自定义标题栏
- Android简单自定义标题栏
- 简单的自定义标题栏(不使用Toolbar)
- 安卓自定义简单的Title标题栏(仿iPhone样式)
- Android开发--自定义颜色渐变的标题栏简单Demo
- 自定义标题栏的问题
- 自定义的顶部标题栏
- 自定义View之简单顶部标题栏
- QT4.7自定义标题栏简单实现
- 菜鸟初学android体验之——实现自定义简单标题栏的两种简单方法
- 自定义Activity的标题栏(Titlebar)
- 自定义Activity的标题栏(Titlebar)
- 自定义Activity的标题栏(Titlebar)
- 我的android_code_lib:自定义标题栏
- 自定义顶部标题栏的view
- 自定义标题栏遇到的问题
- Qt自定义外观--自定义标题栏的窗口
- Qt自定义外观--自定义标题栏的窗口
- 使用数据泵注意事项
- pom.xml
- 基本类型传值和引用类型传值
- 学习struts
- HM TEncCu相关说明
- 简单的自定义标题栏
- 423
- NIO和阻塞io的读写文件效率差异
- Pipeline Scheduling UVA
- 算法题-蛇形填数
- 资源 | 注意迁移的PyTorch实现
- 关于Ajax调用时后台数据的传输问题
- DialogFragment使用
- 人工智能会不会取代开发它的人?