Android TabLayout(选项卡布局)简单用法实例分析
作者:chenguang79 发布时间:2023-12-17 09:52:28
本文实例讲述了Android TabLayout(选项卡布局)简单用法。分享给大家供大家参考,具体如下:
我们在应用viewpager的时候,经常会使用TabPageIndicator来与其配合。达到很漂亮的效果。但是TabPageIndicator是第三方的,而且比较老了,当然了现在很多大神都已经开始自己写TabPageIndicator来满足自己的需求,在2015年的google大会上,google发布了新的Android Support Design库,里面包含了几个新的控件,其中就有一个TabLayout,它就可以完成TabPageIndicator的效果,而且还是官方的,最好的是它可以兼容到2.2以上版本,包括2.2。下面我就举一个简单的例子来使用它。
这里使用的 android studio进行开发的,所以引用TabLayout很简单,只要在build.gradle中加入compile 'com.android.support:design:22.2.0'即可。
这个使用是我在仿 知乎 的时候使用。所以页面就和知乎很像了
fragment_find.xml
<LinearLayout 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"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tab_FindFragment_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/titleBlue"
app:tabIndicatorColor="@color/white"
app:tabSelectedTextColor="@color/gray"
app:tabTextColor="@color/white"
/>
<android.support.v4.view.ViewPager
android:id="@+id/vp_FindFragment_pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>
这里面没有什么特别的,就是添加了一个TabLayout和Viewpager作为上下的布局。其中
app:tabIndicatorColor="@color/white" // 下方滚动的下划线颜色
app:tabSelectedTextColor="@color/gray" // tab被选中后,文字的颜色
app:tabTextColor="@color/white" // tab默认的文字颜色
Find_tab_Adapter.java 它是viewpager的Adapter,因为这里面我每个栏目下,都会有一些列表,所以采用list<View>的方式,在里面切换layout不太适合,所以我采用了List<Fragment>来直接加载多个fragment
package com.example.cg.myzhihu.Adapters;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.List;
/**
* Created by cg on 2015/9/26.
*/
public class Find_tab_Adapter extends FragmentPagerAdapter {
private List<Fragment> list_fragment; //fragment列表
private List<String> list_Title; //tab名的列表
public Find_tab_Adapter(FragmentManager fm,List<Fragment> list_fragment,List<String> list_Title) {
super(fm);
this.list_fragment = list_fragment;
this.list_Title = list_Title;
}
@Override
public Fragment getItem(int position) {
return list_fragment.get(position);
}
@Override
public int getCount() {
return list_Title.size();
}
//此方法用来显示tab上的名字
@Override
public CharSequence getPageTitle(int position) {
return list_Title.get(position % list_Title.size());
}
}
FindFragment.java这个的说法,全在标注里面了
package com.example.cg.myzhihu;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.cg.myzhihu.Adapters.Find_tab_Adapter;
import java.util.ArrayList;
import java.util.List;
/**
* 发现页面
*/
public class FindFragment extends Fragment {
private TabLayout tab_FindFragment_title; //定义TabLayout
private ViewPager vp_FindFragment_pager; //定义viewPager
private FragmentPagerAdapter fAdapter; //定义adapter
private List<Fragment> list_fragment; //定义要装fragment的列表
private List<String> list_title; //tab名称列表
private Find_hotRecommendFragment hotRecommendFragment; //热门推荐fragment
private Find_hotCollectionFragment hotCollectionFragment; //热门收藏fragment
private Find_hotMonthFragment hotMonthFragment; //本月热榜fragment
private Find_hotToday hotToday; //今日热榜fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_find, container, false);
initControls(view);
return view;
}
/**
* 初始化各控件
* @param view
*/
private void initControls(View view) {
tab_FindFragment_title = (TabLayout)view.findViewById(R.id.tab_FindFragment_title);
vp_FindFragment_pager = (ViewPager)view.findViewById(R.id.vp_FindFragment_pager);
//初始化各fragment
hotRecommendFragment = new Find_hotRecommendFragment();
hotCollectionFragment = new Find_hotCollectionFragment();
hotMonthFragment = new Find_hotMonthFragment();
hotToday = new Find_hotToday();
//将fragment装进列表中
list_fragment = new ArrayList<>();
list_fragment.add(hotRecommendFragment);
list_fragment.add(hotCollectionFragment);
list_fragment.add(hotMonthFragment);
list_fragment.add(hotToday);
//将名称加载tab名字列表,正常情况下,我们应该在values/arrays.xml中进行定义然后调用
list_title = new ArrayList<>();
list_title.add("热门推荐");
list_title.add("热门收藏");
list_title.add("本月热榜");
list_title.add("今日热榜");
//设置TabLayout的模式
tab_FindFragment_title.setTabMode(TabLayout.MODE_FIXED);
//为TabLayout添加tab名称
tab_FindFragment_title.addTab(tab_FindFragment_title.newTab().setText(list_title.get(0)));
tab_FindFragment_title.addTab(tab_FindFragment_title.newTab().setText(list_title.get(1)));
tab_FindFragment_title.addTab(tab_FindFragment_title.newTab().setText(list_title.get(2)));
tab_FindFragment_title.addTab(tab_FindFragment_title.newTab().setText(list_title.get(3)));
fAdapter = new Find_tab_Adapter(getActivity().getSupportFragmentManager(),list_fragment,list_title);
//viewpager加载adapter
vp_FindFragment_pager.setAdapter(fAdapter);
//tab_FindFragment_title.setViewPager(vp_FindFragment_pager);
//TabLayout加载viewpager
tab_FindFragment_title.setupWithViewPager(vp_FindFragment_pager);
//tab_FindFragment_title.set
}
}
效果图,不太会做成动态的:
希望本文所述对大家Android程序设计有所帮助。
猜你喜欢
- 简介一般情况下我们在flutter中搭建的app基本上都是用的是MaterialApp这种设计模式,MaterialApp中为我们接下来使用
- 前言已经有两个月没有更新博客了,其实这篇文章我早在两个月前就写好了,一直保存在草稿箱里没有发布出来。原因是有一些原理性的东西还没了解清楚,最
- Java API不能远程访问HBase今天我在虚拟机里面安装了Hbase 1.2.4,说在windows上Java API调用访问下玩玩,结
- 目录写在前面引入guava依赖包怎么做变量转换写在前面有时候需要处理对象属性的getter、setter方法,或者将属性与数据表字段进行相互
- 在我们对程序进行操作过程中,一般都需要有一个操作流程的记录显示。用C#进行编程时可以很容易实现这个功能。本经验提供案例仅供参考下面小编就来介
- MyBatis缓存我们知道,频繁的数据库操作是非常耗费性能的(主要是因为对于DB而言,数据是持久化在磁盘中的,因此查询操作需要通过IO,IO
- kafka消费者kafka-console-consumer接收不到数据发送端接收端问题采用内置的zookeeper,发送端发送数据,接收端
- 针对字符串是数字和字母结合而进行的,如"a20"和"a9";比较而得出结果是"a20&qu
- 1.数据数据(Data)是外部世界信息的载体, 是能够被计算机识别,加工,存储的。在现实生活中也就是我们的产品原材料。计算机中的数据包括数值
- 一、SpringMvc概述SpringMVC是一个基于MVC设计模式的WEB层框架。SpringMVC设计模式:MVC,全名是(Model
- Springboot Aspect切面的实现今天我们来说说spring中的切面Aspect,这是Spring的一大优势。面向切面编程往往让我
- Unity 有点击屏幕进行移动操作,通过Input.GetMouseButtonDown(0)。如果点击到了一些UI上面会触发点击屏幕事件。
- 前言:什么是多数据源?最常见的单一应用中最多涉及到一个数据库,即是一个数据源(Datasource)。那么顾名思义,多数据源就是在一个单一应
- 本文实例为大家分享了java + dom4j.jar提取xml文档内容的具体代码,供大家参考,具体内容如下资源下载页:点击下载本例程主要借助
- 本文实例为大家分享了Android自定义View画圆的具体代码,供大家参考,具体内容如下引入布局<?xml version="
- Spring depends-on的使用通过在XML中的<bean>里配置depends-on属性或者在一个类上使用注解@Dep
- 一、非配置文件注入1、注入普通字符串直接附在属性名上,在 Bean 初始化时,会赋初始值。@Value("admin")
- 本文实例讲述了C#中图片、二进制与字符串的相互转换方法。分享给大家供大家参考,具体如下:protected void Button1_Cli
- 1、定义方法的格式 访问修饰符 返回类型 方法名 (参数列表) { // 方法的主体… }2、静态方法在大多数时候,我们定义写一个方法,会把
- 引言综合应用Java的GUI编程和网络编程,实现一个能够支持多组用户同时使用的聊天室软件。该聊天室具有比较友好的GUI界面,并使用C/S模式