之前我认为自己的脑子还是比较好使的,然后每次遇到问题直接查,但是经过很多次的百度,我发现自己的脑子并不是很好使,因为前脚查完的东西后脚就忘了,这次我准备直接写到自己的笔记里得了。不说废话了直接开始操作步骤。上才艺~~~~
第一步 在自己的Android studio 上New一个Project, 手机的模板就用个简单的就好 Empty Activity,用这个模板就好,在创建到如下图的时候,我们的Minimum SDK尽量选择17就行了,不用太高,如果太高的话有可能再引入XUI的时候出现一些错误,然后我们就可以直接点右下角的finish了,一个空的project用的依赖也比较多,所以可能会等的时间长一点,大家耐心等待就好。 第二步 进入XUI的GitHub地址XUI的github地址: https://github.com/xuexiangjys/XUI.
点击红色框里的wiki 来到这里再点击如何引用 第三步 接下来就是核心了,首先给Gradle添加依赖,当然我图里添加了两个地方,大家就按照我的方式添加就行他俩还是有关系的allprojects呢是所有依赖的仓库,buildscript呢是一个编译脚本的仓库1.先在项目根目录的 build.gradle 的 repositories 添加: allprojects { repositories { ... maven { url "https://jitpack.io" }}} 第四步 在app文件夹里的build.gradle文件中 dependencies { ... //androidx项目 implementation 'com.github.xuexiangjys:XUI:1.1.6' implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.recyclerview:recyclerview:1.1.0' implementation 'com.google.android.material:material:1.1.0' implementation 'com.github.bumptech.glide:glide:4.11.0' 这个依赖在文档中 是4.11.0但是我们只需要下面这个4.8.0就可以了,因为XUI没有做升级,如果使用4.11.0的 话可能会有版本冲突,导致有些组件找不到。 implementation 'com.google.android.material:material:4.8.0'【注意】如果你的项目目前还未使用androidx,请使用如下配置:dependencies { ... //support项目 implementation 'com.github.xuexiangjys:XUI:1.0.9-support' implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support:recyclerview-v7:28.0.0' implementation 'com.android.support:design:28.0.0' implementation 'com.github.bumptech.glide:glide:4.8.0' } 在这之后我们就完成对项目的导入依赖操作了,依赖导完之后一定要点击右上角的sync now按钮应该是重新加载依赖吧 第五步在Application中初始化XUI设置
首先我们要在下图绿色框的包中,新建一个Java Class名字就叫做Myapplication 第六步 使用extends关键字 继承Application然后重写onCreate方法将 XUI.init(this); XUI.debug(true); 写入方法中 第七步 我们需要在AndroidMainfest.xml中写入name属性配置 .Myapplication 前面是有点的,这样项目启动的时侯就会找到我么你创建的Myapplication文件。 第八步 配置XUI的主题,因为XUI的组件都是在XUI的主题上应用的,如果你不配置XUI的主题的话,可能组件也会没有效果。马上完成了,希望大家耐心一点,让我们继续看下去。接下来我们配置XUI的主题基础主题类型:
大平板(10英寸, 240dpi, 1920*1200):XUITheme.Tablet.Big小平板(7英寸, 320dpi, 1920*1200):XUITheme.Tablet.Small手机(4.5英寸, 320dpi, 720*1280):XUITheme.Phone因为我们使用的手机,所以选择 XUITheme.Phone就好了
下图是我已经修改过的默认主题样式,大家根据我的方法替换掉之前的主题就好,之前的style中也有很多颜色,我直接删了,留着也可以,但是没有必要。这个默认的样式在themes.xml文件当中,我呢在两个不同的文件夹下有两个相同的themes.xml文件,所以我都改了。如果你就只有一个那就只改一个就行。
到这里我们的XUI依赖引入就到此为止了。
在MainActivity.java中写入代码
在src➡res➡layout➡activity_main.xml中写入
<?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" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical"> <com.xuexiang.xui.widget.tabbar.EasyIndicator android:id="@+id/easy_indicator" android:layout_width="match_parent" android:layout_height="wrap_content" app:indicator_bottom_line_color="#000000" app:indicator_height="60dp" app:indicator_line_color="#03DAC5" app:indicator_line_show="true" app:indicator_normal_color="#9C9C9C" app:indicator_select_textSize="16sp" app:indicator_selected_color="#03DAC5" app:indicator_textSize="14sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <androidx.viewpager.widget.ViewPager android:id="@+id/view_pager1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="60dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/easy_indicator" > </androidx.viewpager.widget.ViewPager></LinearLayout>
运行之后完成!!!
在最后也祝愿大家在写Android的路上一去不复返,在此博主也作诗一首,希望大家不要嫌弃。