• Android 中ActionBar+fragment实现页面导航、KeyDown事件、自定义主题Style


    为保证android2.0以上均能运行,使用support.v7库下的actionbar及fragment继承自AppCompatActivity(ActionBarActivity已过时)使用getSupportActionBar()得到ActionBar,ActionBar.Tab,这里Tab必须设置监听,在监听中实现Fragment的切换。

    这里重点提一下,Theme主题一定要适配,因为我使用的是AppCompatActivity,后面自定义主题。

    1、main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 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"
        tools:context=".MainActivity">
        <FrameLayout
            android:id="@+id/context"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
    </android.support.constraint.ConstraintLayout>

    2、MainActivity

    package com.example.call_aidl_for_rfid_as;
    
    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v4.app.FragmentTransaction;
    import android.support.v7.app.ActionBar;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.util.Log;
    import android.view.KeyEvent;
    import android.view.View;
    
    public class Main2Activity extends AppCompatActivity {
    
        ActionBar actionBar;
        BaseFragment curFragment;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
    
            actionBar = getSupportActionBar();
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    
            ActionBar.Tab tab1 = actionBar.newTab();
            tab1.setText("新业务Service");
    
            tab1.setTabListener(new MyTabListener(new NewBusinessServiceFragment()));
            ActionBar.Tab tab2 = actionBar.newTab().setText("盘点Service");
            tab2.setTabListener(new MyTabListener(new InventoryServiceFragment()));
            actionBar.addTab(tab1);
            actionBar.addTab(tab2);
        }
    
    
    
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {return curFragment.onKeyDown(keyCode,event);
        }
    
        class MyTabListener implements ActionBar.TabListener{
    
            private BaseFragment fragment;
            public MyTabListener (BaseFragment fragment){
                this.fragment=fragment;
            }
    
    
            @Override
            public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
                curFragment = fragment;
                ft.replace(R.id.context,fragment);
            }
    
            @Override
            public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
    
            }
    
            @Override
            public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
                
            }
        }
    
        @Override
        protected void onDestroy() {
    
            super.onDestroy();
        }
    
    }

    3、BaseFragement.class   用于处理KeyDown事件

    package com.example.rfid_sdk_as;
    import android.support.v4.app.Fragment;
    import android.util.Log;
    import android.view.KeyEvent;
    
    public class BaseFragment extends Fragment{
    
        //这里的是我们的重点
        public boolean onKeyDown(int keyCode, KeyEvent event){return false;
        }
    }

    4、NewBusinessServiceFragment

    public class NewBusinessServiceFragment extends BaseFragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_fragment_new_business, container, false);
        }
         
       @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    
            super.onActivityCreated(savedInstanceState);
            View view = getView();
            context = view.getContext();
    
       }
    
     @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
    
            // TODO Auto-generated method stub
            if (keyCode == 280 || keyCode == 139) {
               
            }
    
            return super.onKeyDown(keyCode, event);
        }
    
    
    }

    5、InventoryServiceFragment

    public class NewBusinessServiceFragment extends BaseFragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_inventory, container, false);
        }
         
       @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    
            super.onActivityCreated(savedInstanceState);
            View view = getView();
            context = view.getContext();
    
       }
    
     @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
    
            // TODO Auto-generated method stub
            if (keyCode == 280 || keyCode == 139) {
               
            }
    
            return super.onKeyDown(keyCode, event);
        }
    
    
    }

    6、AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.rfid_sdk_as">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/CustomActionBarTheme">
    
            <activity android:name=".Main2Activity"
                android:windowSoftInputMode="adjustPan">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
           
            <activity
                android:name=".MainActivity"
                android:windowSoftInputMode="adjustPan">
            </activity>
    
        </application>
    
    </manifest>

    7、配置CustomActionBarTheme主题,在values目录下的style.xml文件中添加如下:

     <style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light">
            <item name="android:actionBarStyle">@style/MyActionBar</item>
            <item name="android:actionBarDivider">@null</item>
            <item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item>
            <!--         <item name="android:actionBarTabStyle">@style/MyActionBarTabs</item> -->
            <item name="android:actionBarTabBarStyle">@style/MyActionBarTabBars</item>
            <item name="android:actionMenuTextColor">@android:color/holo_green_light</item>
    
            <!-- Support library compatibility -->
            <item name="actionBarStyle">@style/MyActionBar</item>
            <item name="actionBarDivider">@null</item>
            <item name="actionBarTabTextStyle">@style/MyActionBarTabText</item>
            <!--         <item name="actionBarTabStyle">@style/MyActionBarTabs</item> -->
            <item name="actionBarTabBarStyle">@style/MyActionBarTabBars</item>
            <item name="actionMenuTextColor">@android:color/holo_green_light</item>
        </style>
    
        <!-- ActionBar styles -->
        <style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar">
            <!--<item name="android:background">@android:color/holo_orange_light</item>-->
            <item name="android:titleTextStyle">@style/MyActionBarTitleText</item>
            <item name="android:backgroundStacked">@color/colorWhite</item>
    
            <!-- Support library compatibility -->
            <item name="background">@color/colorPrimary</item>
            <item name="titleTextStyle">@style/MyActionBarTitleText</item>
            <item name="backgroundStacked">@color/colorWhite</item>
        </style>
    
        <!-- ActionBar title text -->
        <style name="MyActionBarTitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
            <item name="android:textColor">@color/colorWhite</item>
        </style>
    
        <!-- ActionBar tabs text -->
        <style name="MyActionBarTabText" parent="@style/Widget.AppCompat.ActionBar.TabText">
            <item name="android:textColor">@android:color/holo_blue_bright</item>
        </style>
    
        <style name="MyActionBarTabBars" parent="@style/Widget.AppCompat.ActionBar.TabBar">
            <item name="android:showDividers">none</item>
        </style>
  • 相关阅读:
    Javascript 如何识别数组
    新手 如何搭建一个vue项目详解
    javasScript 七种数据类型
    WPF实现动画的几种方式及其小案例
    问题清单
    2020软件工程个人作业06——软件工程实践总结作业
    我的“捡漏”生涯——小黄衫篇
    2020软件工程作业05
    2020软件工程作业03
    Python: list indices must be integers or slices, not float问题
  • 原文地址:https://www.cnblogs.com/zoro-zero/p/12574512.html
Copyright © 2020-2023  润新知