实现功能:
1)定义标题栏布局;
2)自定义TitleActivity控制标题栏按钮监听;
3)在TitleActivity中实现标题栏以下内容区域的切换;
4)新建Activity继承TitleActivity,实现重用标题栏效果。
实现步骤:
1. 新建一个工程,命名为TitleActivityDemo,Activity命名为TitleActivity;
2. 实现如图效果
分为:返回按钮、返回键、标题、提交
3. 设置返回按钮监听,并toast;
1)设置左上角箭头和返回文案区域点击事件的监听,toast提示如图文案;
2)设置提交文字的点击事件监听并toast提示“我是提交按钮”;
4). 实现标题栏下方内容区域;
5)编写MainActivity类继承TitleActivity,实现如图整体逻辑。
效果图:
功能代码:
1.首先定义标题栏 layout_title.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <!-- 首先定义标题栏 -->
- <RelativeLayout
- android:id="@+id/biaoti"
- android:layout_width="match_parent"
- android:layout_height="58dp"
- android:background="#ed4255">
- <TextView
- android:text="标题栏"
- android:id="@+id/text_title"
- android:textSize="21sp"
- android:textColor="#fff"
- android:gravity="center"
- android:singleLine="true"
- android:ellipsize="marquee"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
- <Button
- android:id="@+id/button_backward"
- android:layout_width="70dp"
- android:layout_height="match_parent"
- android:drawableLeft="@drawable/back_arrow"
- android:drawablePadding="6dp"
- android:background="#ed4255"
- android:ellipsize="end"
- android:gravity="center"
- android:onClick="onClick"
- android:paddingLeft="5dp"
- android:singleLine="true"
- android:text="返回"
- android:textColor="#ffffffff"
- android:textSize="18dp"
- android:visibility="invisible" />
- <Button
- android:id="@+id/button_forward"
- android:layout_width="70dp"
- android:layout_height="match_parent"
- android:layout_alignParentRight="true"
- android:drawablePadding="6dp"
- android:background="#ed4255"
- android:ellipsize="end"
- android:gravity="center"
- android:onClick="onClick"
- android:paddingLeft="5dp"
- android:singleLine="true"
- android:text="提交"
- android:textColor="#ffffffff"
- android:textSize="18dp"
- android:visibility="invisible" />
- </RelativeLayout>
- <TextView
- android:text="Android"
- android:textSize="28sp"
- android:gravity="center"
- android:layout_below="@+id/biaoti"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
- </RelativeLayout>
<include>
标签引入标题栏,且下方有定义一个空的FrameLayout的布局。
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <!-- 定义界面的标题栏和标题栏以下内容的布局
- 此处使用<include>标签引入标题栏,且下方有定义一个空的FrameLayout的布局。 -->
- <include layout="@layout/layout_title"></include>
- <FrameLayout
- android:background="#fff"
- android:id="@+id/layout_content"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- </FrameLayout>
- </LinearLayout>
3.TitleActivity.java 定义TitleActivity布局以及按钮
- package com.bwie.title;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.FrameLayout;
- import android.widget.TextView;
- import android.widget.Toast;
- /**
- * 1)定义标题栏布局;
- * 2)自定义TitleActivity控制标题栏按钮监听;
- * 3)在TitleActivity中实现标题栏以下内容区域的切换;
- */
- public class TitleActivity extends Activity implements View.OnClickListener {
- //private RelativeLayout mLayoutTitleBar;
- private TextView mTitleTextView;
- private Button mBackwardbButton;
- private Button mForwardButton;
- private FrameLayout mContentLayout;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setupViews(); //加载 activity_title 布局 ,并获取标题及两侧按钮
- }
- private void setupViews() {
- super.setContentView(R.layout.activity_title);
- mTitleTextView = (TextView) findViewById(R.id.text_title);
- mContentLayout = (FrameLayout) findViewById(R.id.layout_content);
- mBackwardbButton = (Button) findViewById(R.id.button_backward);
- mForwardButton = (Button) findViewById(R.id.button_forward);
- }
- /**
- * 是否显示返回按钮
- *
- * @param backwardResid 文字
- * @param show true则显示
- */
- protected void showBackwardView(int backwardResid, boolean show) {
- if (mBackwardbButton != null) {
- if (show) {
- mBackwardbButton.setText(backwardResid);
- mBackwardbButton.setVisibility(View.VISIBLE);
- } else {
- mBackwardbButton.setVisibility(View.INVISIBLE);
- }
- } // else ignored
- }
- /**
- * 提供是否显示提交按钮
- *
- * @param forwardResId 文字
- * @param show true则显示
- */
- protected void showForwardView(int forwardResId, boolean show) {
- if (mForwardButton != null) {
- if (show) {
- mForwardButton.setVisibility(View.VISIBLE);
- mForwardButton.setText(forwardResId);
- } else {
- mForwardButton.setVisibility(View.INVISIBLE);
- }
- } // else ignored
- }
- /**
- * 返回按钮点击后触发
- * @param backwardView
- */
- protected void onBackward(View backwardView) {
- Toast.makeText(this, "点击返回,可在此处调用finish()", Toast.LENGTH_SHORT).show();
- //finish();
- }
- /**
- * 提交按钮点击后触发
- *
- * @param forwardView
- */
- protected void onForward(View forwardView) {
- Toast.makeText(this, "我是提交按钮", Toast.LENGTH_SHORT).show();
- }
- //设置标题内容
- @Override
- public void setTitle(int titleId) {
- mTitleTextView.setText(titleId);
- }
- //设置标题内容
- @Override
- public void setTitle(CharSequence title) {
- mTitleTextView.setText(title);
- }
- //设置标题文字颜色
- @Override
- public void setTitleColor(int textColor) {
- mTitleTextView.setTextColor(textColor);
- }
- //取出FrameLayout并调用父类removeAllViews()方法
- @Override
- public void setContentView(int layoutResID) {
- mContentLayout.removeAllViews();
- View.inflate(this, layoutResID, mContentLayout);
- onContentChanged();
- }
- @Override
- public void setContentView(View view) {
- mContentLayout.removeAllViews();
- mContentLayout.addView(view);
- onContentChanged();
- }
- /* (non-Javadoc)
- * @see android.app.Activity#setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
- */
- @Override
- public void setContentView(View view, ViewGroup.LayoutParams params) {
- mContentLayout.removeAllViews();
- mContentLayout.addView(view, params);
- onContentChanged();
- }
- /* (non-Javadoc)
- * @see android.view.View.OnClickListener#onClick(android.view.View)
- * 按钮点击调用的方法
- */
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.button_backward:
- onBackward(v);
- break;
- case R.id.button_forward:
- onForward(v);
- break;
- default:
- break;
- }
- }
- }
- package com.bwie.title;
- import android.os.Bundle;
- /**
- * MainActivity中调用时直接 extends TitleActivity,使用在TitleActivity中定义的方法
- */
- public class MainActivity extends TitleActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- setTitle("标题栏");
- showBackwardView(R.string.text_back,true);
- showForwardView(R.string.text_forward,true);
- }
- }
- <resources>
- <string name="text_back">返回</string>
- <string name="text_forward">提交</string>
- </resources>
6.布局中需要引用的左箭头图片
图片