Android自定义顶部栏及侧滑菜单和fragment+viewpag滑动切换的实现
嘿嘿嘿,關(guān)于android滑動(dòng)的操作,是不是經(jīng)常都會(huì)用到呢。
我肯定也要學(xué)習(xí)一下啦。
https://blog.csdn.net/u013184970/article/details/82882107
https://blog.csdn.net/qq_35820350/article/details/82460376
在網(wǎng)上學(xué)習(xí)了一下,這兩篇文章寫的不錯(cuò)。
來(lái)看一下效果
共有4各部分
1.自定義頂部欄
2.側(cè)滑菜單
3.彈出菜單
4.標(biāo)簽滑動(dòng)切換
進(jìn)入具體實(shí)現(xiàn)環(huán)節(jié)啦
先引入v4包和圖片加載包
?
compile 'com.android.support:design:26.1.0'//圖片加載implementation 'com.github.bumptech.glide:glide:4.2.0'?
第一 、自定義頂部欄
1.先要將主題設(shè)置為NoActionBar
2.屁顛屁顛去寫布局咯
三部分構(gòu)成:左邊按鈕,中間標(biāo)題,右邊按鈕
res/layout/layout_top_title.xml
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="50dp"android:background="@android:color/white"><ImageViewandroid:layout_width="45dp"android:layout_height="45dp"android:id="@+id/leftimgview"android:src="@mipmap/tx"android:layout_centerVertical="true"android:padding="10dp"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/mtext"android:text="首頁(yè)"android:textSize="18sp"android:layout_centerInParent="true"android:textColor="@color/colorblack"/><ImageViewandroid:layout_width="45dp"android:layout_height="45dp"android:id="@+id/rightimgview"android:src="@mipmap/menu"android:layout_centerVertical="true"android:layout_alignParentRight="true"android:padding="10dp"/></RelativeLayout>3.在你的activity布局文件中引入
<include layout="@layout/layout_top_title"></include>4.添加點(diǎn)擊監(jiān)聽
activity要實(shí)現(xiàn)View.OnClickListener接口
private ImageView left,right;//左邊按鈕右邊按鈕 left=findViewById(R.id.leftimgview); right=findViewById(R.id.rightimgview);//添加監(jiān)聽 right.setOnClickListener(this); left.setOnClickListener(this);
@Overridepublic void onClick(View view) {switch (view.getId()){case R.id.rightimgview:break;case R.id.leftimgview:}}
再把圖片變成圓角的
//圖片圓角 Glide.with(this).load(R.mipmap.tx).apply(RequestOptions.bitmapTransform(new RoundedCorners(45))).into(left);自定義的頂部欄就大功告成,接下來(lái)實(shí)現(xiàn)側(cè)滑菜單
第二、側(cè)滑菜單
?用到Navigationview(導(dǎo)航視圖)和Drawerlayout(抽屜布局)來(lái)實(shí)現(xiàn)
分析一下:使用抽屜布局,點(diǎn)擊頂部欄左邊按鈕出現(xiàn)側(cè)滑菜單。
側(cè)滑菜單分為兩個(gè)部分,一個(gè)是頭部區(qū)域包括頭像和個(gè)人信息,一個(gè)是菜單區(qū)域。
1.側(cè)滑菜單布局
頭部:res/layout/layout_head.xml ?一張圖片,兩行字
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:gravity="center"android:background="@drawable/shape_gradient"><ImageViewandroid:id="@+id/person_pic"android:layout_width="75dp"android:layout_height="75dp"android:layout_marginTop="42dp"android:src="@mipmap/tx" /><TextViewandroid:id="@+id/companyText"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="24dp"android:text="新人養(yǎng)牛"android:textSize="20sp" /><TextViewandroid:id="@+id/addressText"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginBottom="18dp"android:layout_marginTop="12dp"android:text="在線賣牛奶"android:textSize="16sp" /></LinearLayout>菜單布局 這里要新建一個(gè)menu的文件夾 res/menu/menu_navigation.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"><groupandroid:id="@+id/group1"android:checkableBehavior="single"><itemandroid:id="@+id/group_1"android:icon="@mipmap/myinfo"android:title="我的消息" /><itemandroid:id="@+id/group_2"android:icon="@mipmap/shop"android:title="商城" /><itemandroid:id="@+id/group_3"android:icon="@mipmap/back"android:title="退出登錄" /></group ><itemandroid:id="@+id/item_1"android:icon="@mipmap/vip"android:title="會(huì)員中心" /><itemandroid:id="@+id/item_2"android:icon="@mipmap/setting"android:title="設(shè)置" /></menu>activity布局
<android.support.v4.widget.DrawerLayoutxmlns: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="com.steffenxuan.slide.Main2Activity"android:id="@+id/mdw"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><include layout="@layout/layout_top_title"></include><TextViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:text="hello"/></LinearLayout><android.support.design.widget.NavigationViewxmlns:app="http://schemas.android.com/apk/res-auto"android:id="@+id/nav"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_gravity="left"android:fitsSystemWindows="true"app:headerLayout="@layout/layout_head"app:menu="@menu/menu_navigation"></android.support.design.widget.NavigationView></android.support.v4.widget.DrawerLayout>?2.activity java代碼
private android.support.design.widget.NavigationView navigationview;//導(dǎo)航視圖 private android.support.v4.widget.DrawerLayout drawerlayout;//抽屜 private ImageView person_pic;//側(cè)滑菜單頭部的圖片 private TextView companyText;//側(cè)滑菜單頭部的文字 private TextView addressText;//側(cè)滑菜單頭部的文字private void initFindId() {navigationview=findViewById(R.id.nav);drawerlayout=findViewById(R.id.mdw); }private void initView() { //監(jiān)聽側(cè)滑菜單按鈕點(diǎn)擊navigationview.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {@Overridepublic boolean onNavigationItemSelected(@NonNull MenuItem item) {switch (item.getItemId()){case R.id.group_1:Toast.makeText(MainActivity.this,"我的消息",Toast.LENGTH_SHORT).show();break;case R.id.group_2:Toast.makeText(MainActivity.this,"商城",Toast.LENGTH_SHORT).show();break;case R.id.group_3:Toast.makeText(MainActivity.this,"退出登錄",Toast.LENGTH_SHORT).show();break;case R.id.item_1:Toast.makeText(MainActivity.this,"會(huì)員中心",Toast.LENGTH_SHORT).show();break;case R.id.item_2:Toast.makeText(MainActivity.this,"設(shè)置",Toast.LENGTH_SHORT).show();break;}drawerlayout.closeDrawer(GravityCompat.START);//點(diǎn)擊菜單后關(guān)閉左邊菜單return true;}});}@Overridepublic void onClick(View view) {switch (view.getId()){case R.id.leftimgview:if(drawerlayout.isDrawerOpen(navigationview)){drawerlayout.closeDrawer(navigationview);//關(guān)閉抽屜}else {drawerlayout.openDrawer(navigationview);//打開抽屜 }break;}}最重要的:NavigationView無(wú)法通過(guò)findviewbyid方法獲取header布局
//側(cè)滑菜單中的控件 要先獲取到頭部局才可以if(navigationview.getHeaderCount() > 0){View headerLayout = navigationview.getHeaderView(0);person_pic = headerLayout.findViewById(R.id.person_pic);companyText=headerLayout.findViewById(R.id.companyText);addressText=headerLayout.findViewById(R.id.addressText);}else {View headerLayout = navigationview.inflateHeaderView(R.layout.layout_head);person_pic = headerLayout.findViewById(R.id.person_pic);companyText=headerLayout.findViewById(R.id.companyText);addressText=headerLayout.findViewById(R.id.addressText);}//圓角Glide.with(this).load(R.mipmap.tx).apply(RequestOptions.bitmapTransform(new RoundedCorners(150))).into(person_pic);這篇文章可以看一下 https://www.jianshu.com/p/163e0a25f0aa
第三、彈出菜單
?當(dāng)我們點(diǎn)擊頂部欄右邊按鈕時(shí)彈出菜單
1.菜單布局 res/menu/menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"><item android:id="@+id/one"android:title="第一"app:showAsAction="never"/><item android:id="@+id/two"android:title="第二"app:showAsAction="never"/> </menu>2.activity java代碼
@Overridepublic void onClick(View view) {switch (view.getId()){case R.id.rightimgview:showmenu(view);break;}} public void showmenu(View view){PopupMenu popupMenu=new PopupMenu(Main2Activity.this,view);//實(shí)例化PopupMenugetMenuInflater().inflate(R.menu.menu_main,popupMenu.getMenu());//加載Menu資源//設(shè)置菜單監(jiān)聽popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {@Overridepublic boolean onMenuItemClick(MenuItem item) {switch (item.getItemId()){case R.id.one:Toast.makeText(Main2Activity.this,"one",Toast.LENGTH_LONG).show();return true;case R.id.two:Toast.makeText(Main2Activity.this,"two",Toast.LENGTH_LONG).show();return true;default:return false;}}});popupMenu.show();//顯示menu}?第四、TabLayout+ViewPager+Fragment聯(lián)動(dòng)
ViewPager搭配Fragment去實(shí)現(xiàn)標(biāo)簽頁(yè)是一種非常常見的做法
1.先創(chuàng)建三個(gè)碎片 res/layout/fragment_main.xml
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:text="首頁(yè)"android:gravity="center"/></LinearLayout> public class FragmentMain extends Fragment {@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {View view=inflater.inflate(R.layout.fragment_main,container,false);return view;} }剩下兩個(gè)和這個(gè)一毛一樣,改個(gè)名字就ok
2.添加TabLayout和ViewPager
activity布局,在頂部欄的下面添加
<android.support.design.widget.TabLayoutandroid:background="@android:color/white"android:id="@+id/mtab"android:layout_width="match_parent"android:layout_height="45dp"app:tabIndicatorColor="@android:color/holo_orange_light"app:tabBackground="@android:color/transparent"app:tabGravity="fill"app:tabIndicatorHeight="3dp"android:paddingLeft="5dp"android:paddingRight="5dp"android:paddingBottom="8dp"/><android.support.v4.view.ViewPagerandroid:id="@+id/mvp"android:layout_width="match_parent"android:layout_height="match_parent" />?3.activity Java
private TabLayout tabLayout; private ViewPager viewPager;private void initFindId() {tabLayout = findViewById(R.id.mtab);viewPager = findViewById(R.id.mvp); }private void initViewPage() { //添加適配器viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {@Overridepublic Fragment getItem(int position) {//創(chuàng)建實(shí)例Fragment fragment=new Fragment();if(fragment!=null){switch (position){case 0:fragment=new FragmentMain();break;case 1:fragment=new FragmentSecond();break;case 2:fragment=new FragmentEnd();break;}}return fragment;}@Overridepublic int getCount() {return 3;}});//ViewPager關(guān)聯(lián)到Tablayout中 tabLayout.setupWithViewPager(viewPager);viewPager.setCurrentItem(0); //設(shè)置tabLayouttabLayout.getTabAt(0).setCustomView(getTabView("首頁(yè)"));tabLayout.getTabAt(1).setCustomView(getTabView("另一頁(yè)"));tabLayout.getTabAt(2).setCustomView(getTabView("最后"));//監(jiān)聽tabLayout選中tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {@Overridepublic void onTabSelected(TabLayout.Tab tab) {View view=tab.getCustomView();TextView textView = view.findViewById(R.id.tabtxt);textView.setTextColor(Color.parseColor("#ed8200"));textView.setTextSize(16);textView.getPaint().setFakeBoldText(true);}@Overridepublic void onTabUnselected(TabLayout.Tab tab) {View view = tab.getCustomView();TextView textView = view.findViewById(R.id.tabtxt);textView.setTextColor(Color.parseColor("#999999"));textView.setTextSize(14);}@Overridepublic void onTabReselected(TabLayout.Tab tab) {}});} /*** 獲得Tablayout中tab所在的view* @param titleName* @return*/private View getTabView(String titleName) {View view = LayoutInflater.from(this).inflate(R.layout.layout_tab_item, null);TextView textView = view.findViewById(R.id.tabtxt);textView.setText(titleName);//設(shè)置默認(rèn)選中的viewif (titleName.equals("首頁(yè)")) {textView.setTextColor(Color.parseColor("#ed8200"));textView.setTextSize(16);}return view;}
自定義標(biāo)題布局res/layout/layout_tab_item.xml
可以加入圖標(biāo)
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/tabtxt"/> </LinearLayout>到這里,滑動(dòng)切換就完成了 這里有一篇文章設(shè)置tablayout樣式的
https://www.jianshu.com/p/2b2bb6be83a8
最后
大部分都會(huì)用到這些,剛學(xué)習(xí)android肯定不能落下。
主要是控件Navigationview、Drawerlayout、TabLayout、ViewPager運(yùn)用
Fragment碎片的加入。
?
附上GitHub地址:https://github.com/steffenx/android-
?
轉(zhuǎn)載于:https://www.cnblogs.com/Steffen-xuan/p/11248348.html
總結(jié)
以上是生活随笔為你收集整理的Android自定义顶部栏及侧滑菜单和fragment+viewpag滑动切换的实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
 
                            
                        - 上一篇: IDEA修改Servlet的代码生成模板
- 下一篇: spring -boot定时任务 qua
