商城開發(fā)之自定義頭部TabHost和底部ToolBar

先附上效果圖

ezgif-3-8ac43fbfea.gif

  • 底部導航欄

以前用過LinearLayout+Fragment和TabHost+Activity,感覺用起來很麻煩,正好學習到FragmentTabHost+Fragment,就拿來活學活用,還真提莫好用

官方v4包中的FragmentTabHost會導致Fragment不重用,每次選擇tab后都會重建一個,所以在網(wǎng)上找了個優(yōu)化了的 FragmentTabHost

1.主布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="chong.myshop.MainActivity">

    <!--<chong.myshop.view.MyToolBar-->
    <!--android:id="@+id/toolbar"-->
    <!--android:layout_width="match_parent"-->
    <!--android:layout_height="wrap_content"-->
    <!--android:background="?attr/colorPrimary"-->
    <!--android:minHeight="?actionBarSize"-->
    <!--app:leftButtonIcon="@drawable/icon_back_32px"-->
    <!--app:showSearchView="true"-->
    <!--app:myTitle="首頁"-->
    <!--/>-->

    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/bg_color" />

    <chong.myshop.view.FragmentTabHost
        android:id="@+id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white">

        <FrameLayout
            android:id="@+id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" />

    </chong.myshop.view.FragmentTabHost>
</LinearLayout>

其中realtabcontent是要綁定顯示Fragment的
而寫法怪怪的tabcontent是官方要求

2.新建一個Tab的bean類,其中包含title,icon,fragment屬性并調(diào)出setter和getter方法(as快捷鍵是Alt+Insert),new出五個tab然后放入Tabs數(shù)組(下面有幾個tab就new幾個)

package chong.myshop.bean;

/**
 * Created by chong on 2017/5/3.
 */

public class Tab {
    private int title;
    private int icon;
    private Class fragment;

    public Tab (int title,int icon,Class fragment){
        this.fragment=fragment;
        this.title=title;
        this.icon=icon;
    }

    public int getTitle() {
        return title;
    }

    public void setTitle(int title) {
        this.title = title;
    }

    public int getIcon() {
        return icon;
    }

    public void setIcon(int icon) {
        this.icon = icon;
    }

    public Class getFragment() {
        return fragment;
    }

    public void setFragment(Class fragment) {
        this.fragment = fragment;
    }
}
//新建
Tab home=new Tab(R.string.home,R.drawable.selector_icon_home, HomeFragment.class);
Tab hot=new Tab(R.string.hot,R.drawable.selector_icon_hot, HotFragment.class);
Tab category=new Tab(R.string.category,R.drawable.selector_icon_category, CategoryFragment.class);
Tab cart=new Tab(R.string.cart,R.drawable.selector_icon_cart, CartFragment.class);
Tab mine=new Tab(R.string.mine,R.drawable.selector_icon_mine, MineFragment.class);

其中第二個參數(shù)為R.drawable.selector_icon_home,
因為tab中的圖標也有幾種狀態(tài),所以我們得再drawable里面新建五個selector來實現(xiàn)不同狀態(tài)下的不同圖片.五個Fragment目前只是背景顏色不一樣.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--沒有焦點-->
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@mipmap/icon_cart" />
    <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@mipmap/icon_cartfill_press" />
    <!--有焦點-->
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@mipmap/icon_cartfill_press" />
    <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@mipmap/icon_cartfill_press" />
    <!--按下-->
    <item android:state_selected="true" android:state_pressed="true" android:drawable="@mipmap/icon_cartfill_press" />
    <item android:state_pressed="true" android:drawable="@mipmap/icon_cartfill_press" />
</selector>
//放入數(shù)組
private List<Tab> tabs =new ArrayList<>(5);
tabs.add(home);
tabs.add(hot);
 tabs.add(category);
 tabs.add(cart);
 tabs.add(mine);

3.新建5個TabSpec,并且設置好它的Indicator,再把五個TabSpec放到TabHost中

 myFragmentTabHost.setup(context,getSupportFragmentManager(),R.id.realtabcontent);
  for (Tab tab:tabs){
            TabHost.TabSpec tabSpec=myFragmentTabHost.newTabSpec(getString(tab.getTitle()));
            View view=mInflater.inflate(R.layout.tab_indicator,null);
            TextView title= (TextView) view.findViewById(R.id.tab_title);
            ImageView icon= (ImageView) view.findViewById(R.id.tab_icon);
            title.setText(tab.getTitle());
            icon.setBackgroundResource(tab.getIcon());
            tabSpec.setIndicator(view);
            myFragmentTabHost.addTab(tabSpec,tab.getFragment(),null);
        }

別忘了

//設置第一頁
myFragmentTabHost.setCurrentTab(0);

其中tab_indicator.xml代碼就是一個圖片下面一個文字

<?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"
    android:paddingBottom="3dp"
    android:paddingTop="3dp"
    android:gravity="center"
    android:layout_gravity="center">

    <ImageView
        android:id="@+id/tab_icon"
        android:layout_marginTop="5dp"
        android:layout_width="20dp"
        android:layout_height="20dp" />

    <TextView
        android:id="@+id/tab_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="2dp"
        android:textColor="@color/selector_tab_text"
        />

</LinearLayout>

注意的是TextView的顏色得根據(jù)各種點擊狀態(tài)來改變,所以在color設置了selector ,具體操作就是在res文件目錄下新建color文件夾,再新建selector_tab_text.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:color="#eb4f38" />
    <item android:state_active="true" android:color="#eb4f38"/>
    <item android:state_selected="false" android:color="#a9b7b7" />
    <item android:state_active="false" android:color="#a9b7b7"/>
</selector>

到這里底部導航欄基本已經(jīng)完成,來看看效果

ezgif-3-dbb36fb5b7.gif
  • 接下來寫頭部ToolBar

我們的效果是頭部的標題和搜索框能隨時切換,而原生的ToolBar是沒有搜索框的,所以我們只能自定義一個

先在layout中寫toolbar.xml樣式,由于左邊和右邊都有一個ImageView,而中間是一個EditiView和TextView相互切換,所以我們只要使用相對布局,而EditView和TextView互相隱藏和顯示的就行了

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:paddingRight="10dp"
    android:paddingLeft="10dp">

    <ImageView
        android:layout_width="20dp"
        android:layout_height="20dp"
        style="@style/Base.Widget.AppCompat.Toolbar.Button.Navigation"
        android:id="@+id/toolbar_left_image"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        />

    <ImageView
        style="@style/Base.Widget.AppCompat.Toolbar.Button.Navigation"
        android:id="@+id/toolbar_right_image"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        />

    <EditText
        android:background="@color/brown"
        android:paddingBottom="5dp"
        android:paddingTop="5dp"
        android:id="@+id/toolbar_searchview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="請輸入搜索內(nèi)容"
        android:textSize="15sp"
        android:gravity="center"
        android:drawableLeft="@mipmap/icon_search"
        android:layout_toRightOf="@+id/toolbar_left_image"
        android:layout_toLeftOf="@+id/toolbar_right_image"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"
        style="@style/search_view"

        />

    <TextView
        android:id="@+id/toolbar_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_toRightOf="@+id/toolbar_left_image"
        android:layout_toLeftOf="@+id/toolbar_right_image"
        android:gravity="center"
        android:textColor="@color/white"
        android:textSize="20sp"

        />
</RelativeLayout>

其中EditView的style如下

 <style name="search_view">
        <item name="android:textSize">18sp</item>
        <item name="android:textColor">@color/white</item>
        <item name="android:textColorHint">@color/white</item>
        <item name="android:background">@drawable/selector_search_view</item>
        <item name="android:paddingTop">6dp</item>
        <item name="android:paddingBottom">6dp</item>
        <item name="android:paddingLeft">4dp</item>
        <item name="android:singleLine">true</item>
    </style>

而EditView也有不同狀態(tài)下的監(jiān)聽,所以在background我們引入了@drawable/selector_search_view

<?xml version="1.0" encoding="utf-8"?>

    <selector
        xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_enabled="false" >
            <shape android:shape="rectangle">
                <corners android:radius="8dp" />
                <solid android:color="#eb4f38" />
            </shape>
        </item>
        <item android:state_pressed="true">
            <shape android:shape="rectangle">
                <corners android:radius="8dp" />
                <solid android:color="#eb4f38" />
            </shape>
        </item>
        <item>
            <shape android:shape="rectangle">
                <corners android:radius="8dp" />
                <solid android:color="#D82917" />
            </shape>
        </item>
    </selector>

接下來寫MyToolBar類

  • 新建并繼承ToolBar
  • 三個構造方法中我們要讓它無論如何都是調(diào)用的第三個(也就是三個參數(shù)那個)方法,只需要
public MyToolBar(Context context) {
        this(context, null, 0);
    }

    public MyToolBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyToolBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
}
  • 基本思路就是把toolbar.xml利用原生的addView方法加到MyToolbar
    然后增加自定義屬性,values中新建attrrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="MyToolBar">
        <attr name="showSearchView" format="boolean"/>
        <attr name="leftButtonIcon" format="reference"/>
        <attr name="rightButtonIcon" format="reference"/>
        <attr name="myTitle" format="string"/>
    </declare-styleable>
</resources>

然后在構造方法中用TypedArray拿到attrs中的屬性,
在封裝不同狀態(tài)的各個控件的隱藏與否
最后給各個控件增加監(jiān)聽,代碼很簡單,就直接貼上

package chong.myshop.view;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toolbar;
import chong.myshop.R;

/**
 * Created by chong on 2017/5/3.
 */

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class MyToolBar extends Toolbar {

    private TextView toolbar_title;
    private EditText toolbar_searchview;
    private ImageView toolbar_leftButton;
    private ImageView toolbar_rightButton;
    private View myView;
    private boolean showSearchView;
    private Drawable left_button_icon;
    private Drawable right_button_icon;
    private String title;


    public MyToolBar(Context context) {
        this(context, null, 0);
    }

    public MyToolBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyToolBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);


        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyToolBar,
                defStyleAttr, 0);

        showSearchView = a.getBoolean(R.styleable.MyToolBar_showSearchView, false);
        left_button_icon = a.getDrawable(R.styleable.MyToolBar_leftButtonIcon);
        right_button_icon = a.getDrawable(R.styleable.MyToolBar_rightButtonIcon);
        title = a.getString(R.styleable.MyToolBar_myTitle);

        a.recycle();

        iniView();


    }


    private void iniView() {
        if (myView == null) {
            myView = View.inflate(getContext(), R.layout.toolbar, null);

            toolbar_rightButton = (ImageView) myView.findViewById(R.id.toolbar_right_image);
            toolbar_leftButton = (ImageView) myView.findViewById(R.id.toolbar_left_image);
            toolbar_searchview = (EditText) myView.findViewById(R.id.toolbar_searchview);
            toolbar_title = (TextView) myView.findViewById(R.id.toolbar_title);

            addView(myView);

            if (showSearchView) {
                showSearchView();
                hideTitle();
            } else {
                showTitle();
                hideSearchView();
                if (title != null) {
                    toolbar_title.setText(title);
                }
            }
            if (left_button_icon != null) {
                toolbar_leftButton.setImageDrawable(left_button_icon);
            }
            if (right_button_icon != null) {
                toolbar_rightButton.setImageDrawable(right_button_icon);
            }


            initLinstener();
        }
    }


    /**
     * 標題與搜索框的切換
     */
    public void setShowSearchView() {
        hideTitle();
        showSearchView();
    }

    public void setShowTitleView(String title) {
        hideSearchView();
        showTitle();
        toolbar_title.setText(title);
    }

    /**
     * 設置左右按鈕的圖標
     *
     * @param d
     */
    public void setLeftButtonIconDrawable(Drawable d) {
        toolbar_leftButton.setImageDrawable(d);
    }

    public void setRightButtonIconDrawable(Drawable d) {
        toolbar_rightButton.setImageDrawable(d);
    }

    public void hideSearchView() {
        toolbar_searchview.setVisibility(GONE);
    }

    public void showSearchView() {
        toolbar_searchview.setVisibility(VISIBLE);
    }

    public void hideTitle() {
        toolbar_title.setVisibility(GONE);
    }

    public void showTitle() {
        toolbar_title.setVisibility(VISIBLE);
    }

    @Override
    public void setTitle(int resId) {
        toolbar_title.setText(getContext().getText(resId));

    }

    @Override
    public void setTitle(CharSequence title) {
        toolbar_title.setText(title);
    }

    private OnLeftButtonOnClickListener onLeftButtonOnClickListener;
    private OnRightButtonOnClickListener onRightButtonOnClickListener;


    private void initLinstener() {
        toolbar_rightButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onRightButtonOnClickListener != null) {
                    onRightButtonOnClickListener.onClick();
                }
            }
        });

        toolbar_leftButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onLeftButtonOnClickListener != null) {
                    onLeftButtonOnClickListener.onClick();
                }
            }
        });
    }

    public void setLeftButtonClick(OnLeftButtonOnClickListener listener) {
        onLeftButtonOnClickListener = listener;
    }

    public void setRightButtonClick(OnRightButtonOnClickListener listener) {
        onRightButtonOnClickListener = listener;
    }


    public interface OnLeftButtonOnClickListener {
        void onClick();
    }

    public interface OnRightButtonOnClickListener {
        void onClick();
    }
}

最后把上面layout_main.xml中的MyToolBar取消注釋,運行,大功告成

由于昨天在layout中引用app命名空間的時候手懶復制錯了

xmlns:app="http://schemas.android.com/tools"
xmlns:tools="http://schemas.android.com/tools"

導致頭部的圖片和文字都不顯示,
結果找這個問題花了三四個小時,
找出來后舒暢的喊了一聲cao.

做完感覺原生的圖標很丑,然后在網(wǎng)上找了個

my_icon.png

又舒暢不不少

公司電腦垃圾,沒上傳git

好了,擼代碼去了

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,138評論 25 708
  • 原文地址:http://www.android100.org/html/201606/06/241682.html...
    AFinalStone閱讀 1,304評論 0 1
  • 本來是記得今天是我的生日的,而且是在國外度過的第一個生日!可出差日程推到明天了(晚了一個星期),這幾天忙的也沒顧這...
    FutureToken閱讀 821評論 1 1
  • 第一節(jié) 滿分作文的特點 “中考滿分作文不是沒有缺點的,但肯定是有特點的!” 深圳中考作文從2001年《小樓昨夜又東...
    滬江中小幼閱讀 913評論 0 7

友情鏈接更多精彩內(nèi)容