Android之TabLayout

android中TabLayut到底是个什么鬼?今天我们来看一下,起初,我听见这个名字的时候我总觉得这不是五大布局里的表格布局嘛,现在想想真是有点可笑,下面我们来了解一下它吧!

在2015年的google大会上,google发布了新的Android Support Design库,TabLayout是属于Android Design Support Library中的一个控件,顶部或者底部水平的Tab布局,滑动或者点击切换的功能,今天我们简单讲解TabLayout的使用,重点讲解如何自定义TabLayout的item,也就是每一个tab。 下面我们看一个列子:

这里写图片描述

画圈的自然就是tablayout了,它一共是4个,既可以点击滑动,也可以直接滑动,下面我们就来写代码吧!

首先我们需要在build.gradle中加入compile ‘com.android.support:design:22.2.0’依赖即可。

我打算写3个tab所以先写一个ThirdActivity
代码如下:

ThirdActivity:
package com.example.mytablayoutdemo.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

import com.example.mytablayoutdemo.R;
import com.example.mytablayoutdemo.adapter.SimpleFragmentPagerAdapter;

/**
 * Created by 亮亮 on 2017/7/26.
 */

public class TActivity extends FragmentActivity {

    private SimpleFragmentPagerAdapter pagerAdapter;

    private ViewPager viewPager;

    private TabLayout tabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_t);
        pagerAdapter = new SimpleFragmentPagerAdapter(getSupportFragmentManager(), this);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setAdapter(pagerAdapter);
        tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
    }
}

activity_third布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <android.support.design.widget.TabLayout
        android:id="@+id/sliding_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="scrollable"/>
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1"
        android:background="@android:color/white">

    </android.support.v4.view.ViewPager>
</LinearLayout>
pageFragment代码:
package com.example.mytablayoutdemo.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.mytablayoutdemo.R;

/**
 * Created by 亮亮 on 2017/7/26.
 */

public class PageFragment extends Fragment {

    public static final String ARG_PAGE = "ARG_PAGE";
    private int mPage;

    public static PageFragment newInstance(int page) {
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, page);
        PageFragment pageFragment = new PageFragment();
        pageFragment.setArguments(args);
        return pageFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPage = getArguments().getInt(ARG_PAGE);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.pager_fragment, container, false);
        TextView textView = (TextView) view;
        textView.setText("Fragment #" + mPage);
        return view;
    }
}
这里写代码片
page_fragment布局代码:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:gravity="center">

</TextView>
SimpleFragmentPagerAdapter适配器代码:
package com.example.mytablayoutdemo.adapter;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.example.mytablayoutdemo.fragment.PageFragment;

/**
 * Created by 亮亮 on 2017/7/26.
 */

public class SimpleFragmentPagerAdapter extends FragmentPagerAdapter {
    final int PAGE_COUNT = 3;
    private String tabTitles[] = new String[]{"tab1","tab2","tab3"};
    private Context context;

    public SimpleFragmentPagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.context = context;
    }

    @Override
    public Fragment getItem(int position) {
        return PageFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        return PAGE_COUNT;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return tabTitles[position];
    }
}
需要在androidmanifest配置文件中注册下
<activity android:name=".activity.MainActivity"/>
        <activity android:name=".activity.TActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
        </activity>

到这里代码都写完了,欢迎来参观!下面我们看下效果图吧!

这里写图片描述

写的有点丑还请不要笑奥!!!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: Android自定义TabLayout是指在TabLayout的基础上,通过修改样式、添加图标、自定义布局等方式,实现个性化的TabLayout效果。可以通过修改TabLayout的属性、使用自定义View等方式来实现。 具体实现步骤如下: 1. 在布局文件中添加TabLayout控件,并设置相关属性。 2. 在代码中获取TabLayout控件,并设置TabLayout的样式、添加Tab等。 3. 自定义TabLayout的样式,可以通过修改TabLayout的属性、使用自定义View等方式来实现。 4. 自定义TabLayout的布局,可以通过使用自定义View来实现。 5. 在TabLayout中添加图标,可以通过设置Tab的图标属性来实现。 总之,Android自定义TabLayout可以通过多种方式来实现,具体实现方式取决于具体需求。 ### 回答2: AndroidTabLayout是一个非常常见且重要的控件,用于在界面上实现选项卡切换的效果,TabLayout的默认实现满足了很多场景的需求,但是也有一些场景需要我们自定义TabLayout的样式。 Android自定义TabLayout需要以下步骤: 1. 创建Layout文件 首先,我们需要创建一个布局文件,用于自定义TabLayout的样式。在布局文件中,我们可以自由进行UI设计和排版。 2. 创建自定义TabItem 自定义TabLayout的样式,通常需要创建自定义的TabItem来实现。我们可以通过重写TabItem的布局来实现自定义TabItem的功能。自定义TabItem使我们可以更加灵活地控制选项卡的外观和动画效果。 3. 创建自定义TabLayout 接下来,我们需要创建自定义的TabLayout,我们可以通过继承TabLayout类来实现。在自定义TabLayout中,我们可以添加一些自己的方法和属性,这使得我们可以在代码中更灵活地控制TabLayout。 4. 设置TabLayout的自定义属性 为了使TabLayout支持我们自定义的属性,我们需要在attrs.xml文件中创建自定义属性。 5. 使用自定义TabLayout 最后,我们需要在布局文件中使用自定义TabLayout,并且通过代码调用自定义TabLayout的方法来实现我们想要的效果。 综上所述,Android自定义TabLayout需要一定的UI设计和编程能力,但是通过自定义TabLayout,我们可以实现更加精美和有趣的选项卡效果,从而使我们的应用更加吸引人。 ### 回答3: Android自定义TabLayout是一种可以让开发者自定义TabLayout样式、添加图片、设置字体等等的功能,非常实用。以下是具体使用步骤和过程。 首先,我们需要在布局文件中添加TabLayout,具体代码如下: ``` <com.google.android.material.tabs.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabMode="scrollable" app:tabGravity="center" app:tabIndicatorHeight="2dp" app:tabTextColor="@color/tab_text_color" app:tabSelectedTextColor="@color/colorAccent" app:tabTextAppearance="@style/CustomTabText" /> ``` 其中,我们可以自定义TabLayout的样式,比如指示器高度、字体颜色、大小等等。同时,还可以添加自定义样式的TabTextAppearance,在其中设置自己所需的字体大小、颜色和样式,比如: ``` <style name="CustomTabText" parent="TextAppearance.Design.Tab"> <item name="android:textSize">16sp</item> <item name="android:textColor">@color/tab_text_color</item> <item name="textAllCaps">true</item> </style> ``` 接下来,我们需要在Java文件中设置TabLayout中每个Tab的属性。比如: TabLayout.Tab tab = tabLayout.newTab(); tab.setText("Tab1"); tab.setIcon(R.drawable.tab_1_icon); tabLayout.addTab(tab); 在上述代码中,我们实例化了一个TabLayout.Tab对象,设置了Tab的文本和图标,并将其添加到TabLayout中。 除了图标和文本属性外,我们还可以为每个Tab设置一个自定义View来实现更加个性化的效果。比如: TabLayout.Tab tab = tabLayout.newTab(); View customView = getLayoutInflater().inflate(R.layout.custom_tab_item, null); tab.setCustomView(customView); tabLayout.addTab(tab); 在上述代码中,我们使用了一个自定义布局文件custom_tab_item,将其实例化后并设置为tab的自定义View,从而实现更加个性化的效果。 综上所述,Android自定义TabLayout功能非常强大,开发者可以根据自己的需求自定义TabLayout样式、添加图片、设置字体等等。但也需要注意,自定义过程中需要保证代码的可读性和可维护性,避免出现过多的硬编码和重复代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是阿亮啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值