Android 点击下部有横线的导航

1.自定义view

/*
 *  WHAT Confidential.
 *  OCO Source Materials.
 *  WHAT Equipment Digital Management.
 *  © Copyright WanHe Advanced Technology Ltd. 2019.
 *  The source code for this program is not published or otherwise divested of its trade secrets, irrespective of what has been deposited with the P.R.China Copyright Office.
 */

package com.example.hskeji.utils;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.hskeji.R;


public class TabView extends LinearLayout {

    public TabView(Context context) {
        super(context);
    }

    public TabView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public TabView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private TextView tabview_text;// TabView文本内容
    private TextView tabview_line;// TabView下划线条

    private void initView(Context context){

        tabview_text = new TextView(context);
        LayoutParams textParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        textParams.setMargins(0, ViewUtil.dip2px(context, 20), 0, 0);
        tabview_text.setLayoutParams(textParams);
        tabview_text.setTextColor(getResources().getColor(R.color.tab_button_text_color_u));
        // setTextSize设置字体大小。参数一:字体大小单位;参数二:字体大小号值
        tabview_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
        tabview_text.getPaint().setFakeBoldText(true);// 设置字体加粗
        this.addView(tabview_text);

        tabview_line = new TextView(context);
        LayoutParams lineParams = new LayoutParams(LayoutParams.MATCH_PARENT, ViewUtil.dip2px(context, 5));
        lineParams.setMargins(0, ViewUtil.dip2px(context, 20), 0, ViewUtil.dip2px(context, 5));
        tabview_line.setLayoutParams(lineParams);
        tabview_line.setBackgroundResource(R.color.app_background_color);
        this.addView(tabview_line);

        LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        this.setOrientation(LinearLayout.VERTICAL);
        this.setLayoutParams(layoutParams);
    }

    /**
     * @description: 选中或者不选中TabView,设置其字体色和下划线
     * @createtime: 2019/1/8 15:36
     * @author: surpermind
     * @updatetime: 2019/1/8 15:36
     * @modifier: surpermind
     * @param: isSelect是否选中
     */
    public void setSelect(boolean isSelect){
        // 处理文字色值
        // 处理线条色值
        if (isSelect) {
            tabview_text.setTextColor(getResources().getColor(R.color.tab_button_text_color_h));
            tabview_line.setBackgroundResource(R.color.tab_button_underline_color);
        } else {
            tabview_text.setTextColor(getResources().getColor(R.color.tab_button_text_color_u));
            tabview_line.setBackgroundResource(R.color.white);
        }
    }

    /**
     * @description: 设置TabView的Text值
     * @createtime: 2019/1/9 15:51
     * @author: surpermind
     * @updatetime: 2019/1/9 15:51
     * @modifier: surpermind
     * @param: textValue
     */
    public void setTextValue(String textValue) {
        if (TextUtils.isEmpty(textValue)) {
            textValue = "";
        }
        tabview_text.setText(textValue);
    }
}

/*
 *  WHAT Confidential.
 *  OCO Source Materials.
 *  WHAT Equipment Digital Management.
 *  © Copyright WanHe Advanced Technology Ltd. 2019.
 *  The source code for this program is not published or otherwise divested of its trade secrets, irrespective of what has been deposited with the P.R.China Copyright Office.
 */

package com.example.hskeji.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;

/**
 * @description: 处置屏幕分辨
 * @createtime:
 * @author: surpermind
 * @updatetime:
 * @modifier: surpermind
 * @version: 1.0
 */
public class ViewUtil {

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)<br>
     */
    public static int dip2px(Context context, float dpValue) {
        return (int) (dpValue * getDensity(context) + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp<br>
     */
    public static int px2dip(Context context, float pxValue) {
        return (int) (pxValue / getDensity(context) + 0.5f);
    }

    /**
     * 功能描述:获取当前设备屏幕分辨率<br>
     */
    public static float getDensity(Context context) {
        return context.getResources().getDisplayMetrics().density;
    }

    /**
     * 功能描述:获得屏幕分辨率<br>
     *
     * @param context
     * @return
     */
    public static float getScreenDensity(Context context) {
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.density;
    }

    /**
     * 功能描述:获得屏幕高度<br>
     *
     * @param context
     * @return
     */
    public static int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }

    /**
     * 功能描述:获得屏幕宽度<br>
     *
     * @param context
     * @return
     */
    public static int getScreenHeight(Context context) {
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.heightPixels;
    }

    /**
     * 功能描述:获得状态栏的高度<br>
     *
     * @param context
     * @return
     */
    public static int getStatusHeight(Context context) {

        int statusHeight = -1;
        try {
            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
            Object object = clazz.newInstance();
            int height = Integer.parseInt(clazz.getField("status_bar_height")
                    .get(object).toString());
            statusHeight = context.getResources().getDimensionPixelSize(height);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return statusHeight;
    }

    /**
     * 功能描述:获取当前屏幕截图,包含状态栏<br>
     *
     * @param activity
     * @return
     */
    public static Bitmap snapShotWithStatusBar(Activity activity) {
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bmp = view.getDrawingCache();
        int width = getScreenWidth(activity);
        int height = getScreenHeight(activity);
        Bitmap bp = null;
        bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
        view.destroyDrawingCache();
        return bp;

    }

    /**
     * 功能描述:获取当前屏幕截图,不包含状态栏<br>
     *
     * @param activity
     * @return
     */
    public static Bitmap snapShotWithoutStatusBar(Activity activity) {
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bmp = view.getDrawingCache();
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;

        int width = getScreenWidth(activity);
        int height = getScreenHeight(activity);
        Bitmap bp = null;
        bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height
                - statusBarHeight);
        view.destroyDrawingCache();
        return bp;
    }

}

3.Activity 实现

private void initView() {
        linearHome = (LinearLayout) findViewById(R.id.linear_home);
        tabHome = (TabView) findViewById(R.id.tab_home);
        tabT = (TabView) findViewById(R.id.tab_t);
        tabH = (TabView) findViewById(R.id.tab_h);
        textHome = (TextView) findViewById(R.id.text_home);
        tabHome.setTextValue("首页");
        tabT.setTextValue("购物");
        tabH.setTextValue("我的");
        tabHome.setSelect(true);
        tabH.setSelect(false);
        tabT.setSelect(false);
        tabHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textHome.setText("首页");
                tabHome.setSelect(true);
                tabH.setSelect(false);
                tabT.setSelect(false);
            }
        });
        tabH.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textHome.setText("我的");
                tabHome.setSelect(false);
                tabH.setSelect(true);
                tabT.setSelect(false);

            }
        });
        tabT.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textHome.setText("购物");
                tabHome.setSelect(false);
                tabH.setSelect(false);
                tabT.setSelect(true);
            }
        });
        //扫一扫按钮
        btnSys = (Button) findViewById(R.id.btn_sys);
        btnSys.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }

xml布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <LinearLayout
        android:orientation="horizontal"
        android:id="@+id/linear_home"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_width="match_parent"
        android:layout_height="200dp">
        <com.example.hskeji.utils.TabView
            android:id="@+id/tab_home"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <View
            android:layout_width="50dp"
            android:layout_height="50dp"/>
        <com.example.hskeji.utils.TabView
            android:id="@+id/tab_t"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <View
            android:layout_width="50dp"
            android:layout_height="50dp"/>
        <com.example.hskeji.utils.TabView
            android:id="@+id/tab_h"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    <TextView
        android:id="@+id/text_home"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:textSize="30dp"
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="50dp"/>
    <Button
        android:id="@+id/btn_sys"
        android:text="扫一扫"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/text_home"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</androidx.constraintlayout.widget.ConstraintLayout>
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,470评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,393评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,577评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,176评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,189评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,155评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,041评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,903评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,319评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,539评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,703评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,417评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,013评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,664评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,818评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,711评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,601评论 2 353

推荐阅读更多精彩内容