使用Fragment --- 自定义底部按钮导航

Main_Active.java

package com.panhua.music;

import androidx.annotation.ColorRes;

import androidx.appcompat.app.AppCompatActivity;

import androidx.fragment.app.Fragment;

import androidx.fragment.app.FragmentManager;

import androidx.fragment.app.FragmentTransaction;

import androidx.viewpager.widget.PagerAdapter;

import androidx.viewpager.widget.ViewPager;

import android.content.Intent;

import android.gesture.Gesture;

import android.gesture.GestureLibraries;

import android.gesture.GestureLibrary;

import android.gesture.GestureOverlayView;

import android.gesture.Prediction;

import android.graphics.Color;

import android.graphics.ColorFilter;

import android.graphics.ColorSpace;

import android.graphics.PorterDuff;

import android.graphics.Region;

import android.graphics.drawable.Drawable;

import android.net.Uri;

import android.os.Bundle;

import android.os.Handler;

import android.text.Layout;

import android.text.method.Touch;

import android.util.Log;

import android.view.Display;

import android.view.GestureDetector;

import android.view.Gravity;

import android.view.KeyEvent;

import android.view.MotionEvent;

import android.view.View;

import android.view.ViewGroup;

import android.view.ViewTreeObserver;

import android.view.WindowManager;

import android.view.animation.Animation;

import android.view.animation.ScaleAnimation;

import android.view.animation.TranslateAnimation;

import android.widget.Button;

import android.widget.EditText;

import android.widget.FrameLayout;

import android.widget.ImageButton;

import android.widget.ImageView;

import android.widget.LinearLayout;

import android.widget.RelativeLayout;

import android.widget.TextView;

import android.widget.Toast;

import java.lang.reflect.Array;

import java.util.ArrayList;

import java.util.GregorianCalendar;

import java.util.List;

import java.util.Properties;

public class Main_Active extends AppCompatActivity implements View.OnClickListener {

    private FrameLayout frameLayout;

    private LinearLayout Drawer_Top;

    private The_global_method the_global_method;

    private int Exit_index = 1;

    public void Layout_show_hide(final boolean bo) {

        int status_height = getResources().getDimensionPixelSize(getResources().getIdentifier("status_bar_height", "dimen", "android"));

        TranslateAnimation translateAnimation;

        if (bo) {

            translateAnimation = new TranslateAnimation(0, 0, 0,

                    getWindowManager().getDefaultDisplay().getHeight() - status_height);

        } else {

            findViewById(R.id.Bottom_Layout).setVisibility(View.VISIBLE);

            translateAnimation = new TranslateAnimation(0, 0, 0,

                    -getWindowManager().getDefaultDisplay().getHeight() - status_height);

        }

        translateAnimation.setDuration(500);

        translateAnimation.setFillAfter(true);

        Drawer_Top.startAnimation(translateAnimation);

        translateAnimation.setAnimationListener(new Animation.AnimationListener() {

            @Override

            public void onAnimationStart(Animation animation) {

            }

            @Override

            public void onAnimationEnd(Animation animation) {

                if (bo) {

                    findViewById(R.id.Bottom_Layout).setVisibility(View.GONE);

                    Drawer_Top.clearAnimation();

                    RelativeLayout.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) Drawer_Top.getLayoutParams();

                    marginLayoutParams.setMargins(0, 0, 0, 0);

                    Drawer_Top.setLayoutParams(marginLayoutParams);

                } else {

                    Drawer_Top.clearAnimation();

                    RelativeLayout.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) Drawer_Top.getLayoutParams();

                    marginLayoutParams.setMargins(0, -getWindowManager().getDefaultDisplay().getHeight(), 0, 0);

                    Drawer_Top.setLayoutParams(marginLayoutParams);

                }

            }

            @Override

            public void onAnimationRepeat(Animation animation) {

            }

        });

    }

    public void Drawer_top() {

        RelativeLayout.MarginLayoutParams layoutParams = (RelativeLayout.MarginLayoutParams) Drawer_Top.getLayoutParams();

        layoutParams.width = getWindowManager().getDefaultDisplay().getWidth();

        layoutParams.height = getWindowManager().getDefaultDisplay().getHeight();

        layoutParams.setMargins(0, -getWindowManager().getDefaultDisplay().getHeight(), 0, 0);

        Drawer_Top.setLayoutParams(layoutParams);

    }

    //    按钮Drawer图片设定

    public void Button_Drawable_ico(int view_id, int img_id, boolean boolen) {

        Button view = findViewById(view_id);

        Drawable drawable = getResources().getDrawable(img_id);

        int drawable_top = new The_global_method(getApplicationContext()).px_To_dp(5);

        int drawable_width = new The_global_method(getApplicationContext()).px_To_dp(20);

        int drawable_height = new The_global_method(getApplicationContext()).px_To_dp(25);

        drawable.setBounds(0, drawable_top, drawable_width, drawable_height);

        if (boolen) {

            drawable.setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_ATOP);

        } else {

            drawable.setColorFilter(Color.parseColor("#FFFFFF"), PorterDuff.Mode.SRC_ATOP);

        }

        view.setCompoundDrawables(null, drawable, null, null);

    }

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main_active);

        Drawer_Top = findViewById(R.id.drawer_top);

        Drawer_top();

        findViewById(R.id.button1).setOnClickListener(this);

        findViewById(R.id.button2).setOnClickListener(this);

        findViewById(R.id.button3).setOnClickListener(this);

        findViewById(R.id.button4).setOnClickListener(this);

        findViewById(R.id.button1).callOnClick();

        findViewById(R.id.tools_top).setOnClickListener(this);

        findViewById(R.id.frame_exit).setOnClickListener(this);

        the_global_method = new The_global_method(getApplicationContext());

    }

    @Override

    public void onWindowFocusChanged(boolean hasFocus) {

        super.onWindowFocusChanged(hasFocus);

        Drawable drawable = getResources().getDrawable(R.drawable.main_input_drawable);

        drawable.setBounds(

                findViewById(R.id.input_seek).getWidth()/2 - the_global_method.px_To_dp(25),

                0,

                findViewById(R.id.input_seek).getWidth()/2 - the_global_method.px_To_dp(5),

                the_global_method.px_To_dp(20));

        ((Button) findViewById(R.id.input_seek)).setCompoundDrawables(drawable,null,null,null);

    }

    public void ico_true(int index) {

        Button_Drawable_ico(R.id.button1, R.drawable.main_button1, false);

        Button_Drawable_ico(R.id.button2, R.drawable.main_button2, false);

        Button_Drawable_ico(R.id.button3, R.drawable.main_button3, false);

        Button_Drawable_ico(R.id.button4, R.drawable.main_button4, false);

        ((Button) findViewById(R.id.button1)).setTextColor(Color.parseColor("#FFFFFF"));

        ((Button) findViewById(R.id.button2)).setTextColor(Color.parseColor("#FFFFFF"));

        ((Button) findViewById(R.id.button3)).setTextColor(Color.parseColor("#FFFFFF"));

        ((Button) findViewById(R.id.button4)).setTextColor(Color.parseColor("#FFFFFF"));

        switch (index) {

            case 1:

                Button_Drawable_ico(R.id.button1, R.drawable.main_button1, true);

                ((Button) findViewById(R.id.button1)).setTextColor(Color.BLUE);

                getFragmentManager().beginTransaction().replace(R.id.active_frame, new infomation()).commit();

                break;

            case 2:

                Button_Drawable_ico(R.id.button2, R.drawable.main_button2, true);

                ((Button) findViewById(R.id.button2)).setTextColor(Color.BLUE);

                getFragmentManager().beginTransaction().replace(R.id.active_frame, new contact()).commit();

                break;

            case 3:

                Button_Drawable_ico(R.id.button3, R.drawable.main_button3, true);

                ((Button) findViewById(R.id.button3)).setTextColor(Color.BLUE);

                getFragmentManager().beginTransaction().replace(R.id.active_frame, new hotbroadcast()).commit();

                break;

            case 4:

                Button_Drawable_ico(R.id.button4, R.drawable.main_button4, true);

                ((Button) findViewById(R.id.button4)).setTextColor(Color.BLUE);

                getFragmentManager().beginTransaction().replace(R.id.active_frame, new live()).commit();

                break;

        }

    }

    @Override

    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.frame_exit:

                Layout_show_hide(false);

                break;

            case R.id.tools_top:

                Layout_show_hide(true);

                break;

            case R.id.button1:

                ico_true(1);

                break;

            case R.id.button2:

                ico_true(2);

                break;

            case R.id.button3:

                ico_true(3);

                break;

            case R.id.button4:

                ico_true(4);

                break;

        }

    }

    //    两次返回

    @Override

    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {

            if (Exit_index == 2) {

                finish();

                return super.onKeyDown(keyCode, event);

            } else {

                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();

                Exit_index += Exit_index;

                new Thread(new Runnable() {

                    @Override

                    public void run() {

                        try {

                            Thread.sleep(2000);

                        } catch (Exception e) {

                        }

                        Exit_index = 1;

                    }

                }).start();

            }

        }

        return false;

    }

}


The_global_method.java


package com.panhua.music;

import android.content.Context;

import android.graphics.drawable.Drawable;

import android.os.Bundle;

import android.os.Handler;

import android.os.Looper;

import android.os.NetworkOnMainThreadException;

import android.text.Layout;

import android.util.Log;

import android.view.Gravity;

import android.view.KeyEvent;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.Button;

import android.widget.EditText;

import android.widget.GridLayout;

import android.widget.TextView;

import android.widget.Toast;

import androidx.annotation.Dimension;

import androidx.annotation.Nullable;

import androidx.appcompat.app.AppCompatActivity;

import androidx.viewpager.widget.ViewPager;

import org.json.JSONObject;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.lang.reflect.Array;

import java.net.HttpURLConnection;

import java.net.URL;

/**

* Created by Administrator on 2021/7/2 0002.

*/

public class The_global_method extends AppCompatActivity{

    Context context;

    View view_layout;

    int Exit_index;

    String TAG = "程序日志";

    String server_string;

    JSONObject server_json;

    int server_code;

    //    主函数

    public The_global_method(Context context) {

        this.context = context;

        this.Exit_index = 1;

    }

    //  屏幕dp转px

    public int px_To_dp(int dp_number) {

        float px = context.getResources().getDisplayMetrics().density;

        return (int) (dp_number * px + 0.5f);

    }

    public HttpURLConnection Url(int Port, String Json) {

        HttpURLConnection httpURLConnection = null;

        try {

            URL url = new URL("http://103.45.144.254:" + Port + "/?" + Json);

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.setConnectTimeout(3000);

            connection.setReadTimeout(3000);

            connection.setRequestMethod("GET");

            connection.setDoInput(true);

            connection.setDoOutput(false);

            connection.setUseCaches(false);

            httpURLConnection = connection;

        } catch (Exception e) {

            Log.d(TAG, "Url: " + e);

        }

        return httpURLConnection;

    }

    public void Server(final int Port, final String Json) {

        final HttpURLConnection httpURLConnection = Url(Port, Json);

        new Thread(new Runnable() {

            @Override

            public void run() {

                try {

                    server_code = httpURLConnection.getResponseCode();

                    if (httpURLConnection.getResponseCode() == 200) {

                        InputStream inputStream = httpURLConnection.getInputStream();

                        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

                        int len = 0;

                        byte[] bytes = new byte[1024];

                        while ((len = inputStream.read(bytes)) != -1) {

                            byteArrayOutputStream.write(bytes, 0, len);

                        }

                        inputStream.close();

                        byteArrayOutputStream.close();

                        String string = new String(byteArrayOutputStream.toByteArray());

                        JSONObject jsonObject = new JSONObject(string.toString());

                        server_string = string;

                        server_json = jsonObject;

                    }

                } catch (Exception e) {

                    Log.d(TAG, "Server: " + e);

                }

            }

        }).start();

    }

}

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

推荐阅读更多精彩内容