Tablayout踩坑与实践

2019-11-18  本文已影响0人  汤欧尼

新手菜鸡一枚,跟着教程学Tablayout,结果第一步依赖包导入就遇坑。目标是可以使用android.support.design.widget.TabLayout及android.support.design.widget.TabItem。但是总是提示如下图(就是依赖没导成功呗~):


类缺失提示.png

我的环境:
AS:3.4.1
gradle:3.4.1

一、依赖包引入

尝试方案一:

//TabLayout
compile "com.android.support:design:24.2.0" 

尝试方案二:

//TabLayout
implementation "com.android.support:design:28.0.0" 

尝试方案三:

//TabLayout
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support:support-v4:27.1.1'

尝试方案四:

//TabLayout
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:appcompat-v7:28.0.0'

咳咳,以上方案我都不行~~
最后成功方案五:

//TabLayout(最后导包方案)
implementation 'com.google.android.material:material:1.0.0'

不过这样使用Tablayout就不能用android.support.design.widget.TabLayout及android.support.design.widget.TabItem了;而是要写成com.google.android.material.tabs.TabLayout及com.google.android.material.tabs.TabItem。我也不知道两者的区别是什么,但是不管了,先起码出tab让我继续下去吧~

原因好像是介个:https://blog.csdn.net/guolin_blog/article/details/97142065

布局文件中内容如下(标签上的文字我都配置在strings.xml文件中啦~):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.smartbutler.MainActivity">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/butler_fragment"/>

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/wechat_fragment"/>

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/girl_fragment"/>

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/user_fragment"/>

    </com.google.android.material.tabs.TabLayout>

</LinearLayout>

上一篇 下一篇

猜你喜欢

热点阅读