比较全面的一个自定义导航栏

来源:互联网 发布:黄金交易软件排名 编辑:程序博客网 时间:2024/05/01 07:47

            之前有看过别人写的导航栏,还行,较简便,但是不是很全面,正对自己的项目,我们做了很多的添加和修改,今天想写写东西,现在贡献给大家,希望大家喜欢,大家拿到后可以根据自己的项目需要有针对性的修改就ok。

package com.vqiao.base;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.provider.Settings;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.vqiao.R;
import com.vqiao.base.control.ClearEditText;
import com.vqiao.common.NetworkUtil;

/**
 * Class Name: MsfNaviActivity.java Function: 带导航栏的基类 Modifications:
 * 
 */
public abstract class MsfNaviActivity extends MsfActivity {
 /**
  * 导航布局1
  */
 private LinearLayout navigationLayout;
 /**
  * 导航左边按键(返回)
  */
 private ImageView btnLeft;
 /**
  * 导航中间布局容器
  */
 private LinearLayout layoutMiddle;
 /**
  * 导航右边按键
  */
 private ImageView btnRight;
 /**
  * 右边布局
  */
 private LinearLayout layoutRight;
 /**
  * 主体内容布局
  */
 private LinearLayout contentLayout;
 /**
  * 内容区域视图
  */
 private View contentView;
 /**
  * 网络异常的界面
  */
 private View netExceptionView;
 /**
  * 网络异常时的刷新接口
  */
 protected OnRefreshListener refreshListener;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.navigation_activity);
  navigationLayout = (LinearLayout) findViewById(R.id.navigation_layout);
  btnLeft = (ImageView) navigationLayout.findViewById(R.id.window_back);
  layoutMiddle = (LinearLayout) navigationLayout.findViewById(R.id.navigation_middle);
  layoutRight = (LinearLayout) navigationLayout.findViewById(R.id.navigation_right);
  btnRight = (ImageView) layoutRight.findViewById(R.id.navigation_right_img);
  this.addToBack(btnLeft); // 关闭window
  contentLayout = (LinearLayout) findViewById(R.id.content_layout);
  //buttonOnClick();
  
  //检测到无网络
  if (!NetworkUtil.isNetworkConnected(this)) {
   netExceptionView = findViewById(R.id.network_load_failure_refresh_layout);
   netExceptionView.setVisibility(View.VISIBLE);
   findViewById(R.id.go_system_setting_activity).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
     startActivity(new Intent(Settings.ACTION_SETTINGS));
    }
   });
   findViewById(R.id.refresh_home_date).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
     // 刷新完成后,网络异常界面隐藏
     if (refreshListener != null) {
      refreshListener.onRefresh();
     }
    }
   });
  }
 }

 /***
  * 设置内容区域
  *
  * @param resId
  *            资源文件ID
  */
 public void setContentLayout(int resId) {
  LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  contentView = inflater.inflate(resId, null);
  LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
    LayoutParams.MATCH_PARENT);
  contentView.setLayoutParams(layoutParams);
  // contentView.setBackgroundDrawable(null);

  setContentLayout(contentView);

 }

 /***
  * 设置内容区域
  *
  * @param view
  *            View对象
  */
 public void setContentLayout(View view) {
  if (null != contentLayout) {
   contentLayout.removeAllViews();
   contentLayout.addView(view);
  }
 }

 /**
  * 得到内容的View
  *
  * @return
  */
 public View getLyContentView() {
  return contentView;
 }

 /**
  * 得到左边的按钮
  *
  * @return
  */
 public ImageView getLeftButton() {
  return btnLeft;
 }
 
 public LinearLayout getRightLayout(){
  return layoutRight;
 }

 /**
  * 得到右边的按钮
  *
  * @return
  */
 public ImageView getRightButton() {
  return btnRight;
 }

 /**
  * 设置标题
  *
  * @param title
  */
 public void setTitle(String title) {
  setTitle(title, Color.WHITE);
 }
 /**
  * 设置标题
  *
  * @param title
  * @param color
  */
 public void setTitle(String title, int color) {
  TextView tvTitle = new TextView(this);
  tvTitle.setText(title);
  tvTitle.setGravity(Gravity.CENTER);
  tvTitle.setTextColor(color);
  tvTitle.setTextSize(18);
  if (null != layoutMiddle) {
   layoutMiddle.removeAllViews();
   layoutMiddle.addView(tvTitle);
  }
 }

 /**
  * 设置标题
  *
  * @param resId
  */
 public void setTitle(int resId) {
  String title = getString(resId);
  setTitle(title);
 }

 /**
  * 自定义中间布局
  *
  * @param resId
  */
 public void setMiddleLayout(int resId) {
  LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  View middleView = inflater.inflate(resId, null);
  setMiddleLayout(middleView);
 }

 public void setMiddleLayout(View view) {
  if (null != layoutMiddle) {
   LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
   layoutMiddle.removeAllViews();
   layoutMiddle.addView(view, lp);
  }
 }

 /**
  * 设置左边按钮的图片资源
  *
  * @param resId
  */
 public void setLeftButtonBackground(int resId) {
  if (null != btnLeft) {
   btnLeft.setBackgroundResource(resId);
  }

 }

 /**
  * 设置左边按钮的图片资源
  *
  * @param bm
  */
 public void setLeftButtonBackground(Drawable drawable) {
  if (null != btnLeft) {
   btnLeft.setBackgroundDrawable(drawable);
  }
 }

 public void setLeftImage(int resId) {
  if (null != btnLeft) {
   btnLeft.setImageResource(resId);
  }
 }

 /**
  * 设置右边按钮的图片资源
  *
  * @param resId
  */
 public void setRightBackground(int resId) {
  if (null != btnRight) {
   btnRight.setBackgroundResource(resId);
  }
 }

 public void setRightImage(int resId) {
  if (null != btnRight) {
   btnRight.setImageResource(resId);
  }
 }

 /**
  * 设置右边按钮的图片资源
  *
  * @param drawable
  */
 public void setRightBackground(Drawable drawable) {
  if (null != btnRight) {
   btnRight.setBackgroundDrawable(drawable);
  }
 }
 
 /**
  * 设置右边的文本,如果“搜索”,“提交”
  */
 public void setRightText(String text) {
  LinearLayout rightLayout = getRightLayout();
  rightLayout.removeAllViews();
  TextView tvSearchBtn = new TextView(this);
  tvSearchBtn.setTextColor(0XFFFFFFFF);
  tvSearchBtn.setText(text);
  rightLayout.addView(tvSearchBtn);
 }
 /**
  * 设置右边的文本,如果“搜索”,“提交”
  */
 public void setRightText(TextView tvSearchBtn) {
  LinearLayout rightLayout = getRightLayout();
  rightLayout.removeAllViews();
  rightLayout.addView(tvSearchBtn);
 }

 /**
  * 隐藏上方的标题栏
  */
 public void hideNavigation() {
  if (null != navigationLayout) {
   navigationLayout.setVisibility(View.GONE);
  }
 }
 /**
  * 显示上方的标题栏
  */
 public void showNavigation() {
  if (null != navigationLayout) {
   navigationLayout.setVisibility(View.VISIBLE);
  }
 }

 /**
  * 设置导航栏的背景
  */
 public void setNavigationBg() {
  if (null != navigationLayout) {
   navigationLayout.setVisibility(View.VISIBLE);
   navigationLayout.setBackgroundResource(R.color.navigation_bg);
  }
 }

 /**
  * 隐藏左边的按钮
  */
 public void hideLeftButton() {
  if (null != btnLeft) {
   btnLeft.setVisibility(View.GONE);
  }
 }

 /***
  * 隐藏右边的按钮
  */
 public void hideRightButton() {
  if (null != layoutRight) {
   layoutRight.setVisibility(View.GONE);
  }
 }
 
 public void disableRightButton(){
  if (null != layoutRight) {
   layoutRight.setVisibility(View.INVISIBLE);
  }
 }

 /**
  * 获取导航栏的高度
  */
 public int getNavigationHeight() {
  return navigationLayout.getHeight();
 }
 /**
  * 设置在导航居中的Title
  */
 public void setCenterTitle(int resId, int textColor) {
  String title = getResources().getString(resId);
  setCenterTitle(title, textColor);
 }

 public void setCenterTitle(int resId) {
  setCenterTitle(resId, Color.WHITE);
 }

 public void setCenterTitle(String title) {
  setCenterTitle(title, Color.WHITE);
 }
 
 public void setCenterTitle(String title, int textColor) {
  hideLeftButton();
  setMiddleLayout(R.layout.navigation_center_title);
  addToBack(R.id.backBtAtCenterLeft);
  hideRightButton();
  TextView v = (TextView) findViewById(R.id.navi_bar_title);
  v.setTextColor(textColor);
  v.setText(title);
 }
 
 public String getKeyword() {
  View view = layoutMiddle.findViewById(R.id.navi_activity_middle_edit);
  if (view != null) {
   return ((ClearEditText) view).getText().toString();
  } else {
   return "";
  }
 }
 public void setKeyword(String keywords) {
  View view = layoutMiddle.findViewById(R.id.navi_activity_middle_edit);
  if (view != null) {
   ((ClearEditText) view).setText(keywords);
  }
 }

 public void setWaitforView(boolean isStart){
  RelativeLayout waitforView = (RelativeLayout)findViewById(R.id.activity_waitfor_progress);
  if(isStart){
   waitforView.setVisibility(View.VISIBLE);
  }else{
   waitforView.setVisibility(View.GONE);
  }
 }
 
 public void setWaitforfullView(boolean isStart){
  RelativeLayout waitforView = (RelativeLayout)findViewById(R.id.activity_waitfor_progress_full);
  if(isStart){
   waitforView.setVisibility(View.VISIBLE);
  }else{
   waitforView.setVisibility(View.GONE);
  }
 }
 /**
  * 数据加载成功后,隐藏网络异常界面
  */
 public void setNetExceptionViewGone(){
  if (netExceptionView != null) {
   netExceptionView.setVisibility(View.GONE);
  }
 }
 
 public interface OnRefreshListener{
  public void onRefresh();
 }
 /**
  * 网络异常时,点击刷新数据按钮的回调
  */
 public void setOnRefreshListener(OnRefreshListener r) {
  this.refreshListener = r;
 }
 
 
}

<?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:gravity="center_vertical"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/navigation_layout"
        android:layout_width="match_parent"
        android:layout_height="@dimen/home_title_search_bar_height"
        android:background="@color/msf_home_titlebar_background_color"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/window_back"
            style="@style/textview_white"
            android:layout_width="@dimen/home_title_bar_left_width"
            android:layout_height="40dp"
            android:layout_gravity="center_vertical"
            android:src="@drawable/activity_back" />

        <LinearLayout
            android:id="@+id/navigation_middle"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="horizontal" >

            <!-- 默认 -->

            <com.vqiao.base.control.ClearEditText
                android:id="@+id/navi_activity_middle_edit"
                style="@style/clear_edittext_style"
                android:paddingLeft="5dp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/navigation_right"
            android:layout_width="40dip"
            android:layout_height="40dp"
            android:gravity="center">

            <ImageView
                android:id="@+id/navigation_right_img"
                style="@style/textview_white"
                android:layout_margin="10dip"
                android:src="@drawable/navigation_right_menu" />
        </LinearLayout>
    </LinearLayout>
    <FrameLayout android:layout_width="match_parent"
      android:layout_height="match_parent">
     <LinearLayout
         android:id="@+id/content_layout"
         style="@style/baseLinearLayout"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:gravity="center_horizontal" >
     </LinearLayout>
     <RelativeLayout android:id="@+id/activity_waitfor_progress"
         android:layout_width="150dp"
         android:layout_height="130dp"
         android:background="@drawable/waitfor_bg_window"
         android:layout_gravity="center"
         android:visibility="gone">

            <include layout="@layout/waitfor"
                android:layout_centerInParent="true"
                android:layout_marginBottom="50dp"/>
     </RelativeLayout>
     <RelativeLayout android:id="@+id/activity_waitfor_progress_full"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:background="@color/white"
         android:layout_gravity="center"
         android:visibility="gone">

            <include layout="@layout/waitfor"
                android:layout_centerInParent="true"
                android:layout_marginBottom="50dp"/>
     </RelativeLayout>
     <!-- 无网络提示 -->
     <LinearLayout
            android:id="@+id/network_load_failure_refresh_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white"
            android:gravity="center_horizontal"
            android:orientation="vertical"
            android:visibility="gone" >

         <ImageView
             android:layout_width="match_parent"
             android:layout_height="180dp"
             android:scaleType="fitXY"
             android:layout_marginTop="30dp"
             android:src="@drawable/netslow_show" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:background="#F2EDF1"
                android:gravity="center"
                android:orientation="horizontal">
                   <LinearLayout android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       android:orientation="vertical"
                       android:layout_marginRight="50dp"
                       android:layout_marginLeft="35dp"
                       android:layout_weight="1"
                       android:gravity="left|center_vertical">
                     <TextView  android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:gravity="left"
                         android:textSize="14sp"
                         android:textColor="#935086"
                         android:text="若重新加载还不行," />
                     <TextView  android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:textColor="#935086"
                         android:textSize="14sp"
                         android:gravity="left"
                         android:text="请去检查网络设置。" />
                    </LinearLayout>
                    <Button
                        android:id="@+id/go_system_setting_activity"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="35dp"
                        android:textSize="14sp"
                        android:gravity="center"
                        android:textColor="@color/white"
                        android:background="@drawable/button_red_shap5"
                        android:text="网络设置" />
            </LinearLayout>

             <Button
                android:id="@+id/refresh_home_date"
                style="@style/textview_white"
                android:layout_width="match_parent"
                android:layout_marginLeft="35dip"
                android:layout_marginRight="35dip"
                android:layout_marginTop="40dp"
                android:paddingTop="10dip"
                android:paddingBottom="10dip"
                android:background="@drawable/button_red_shap5"
                android:gravity="center"
                android:text="重新加载" />
        </LinearLayout>
 </FrameLayout>
</LinearLayout>

 

 

0 0
原创粉丝点击