Android Material风格的应用(一)--AppBar TabLayout
打造Material风格的Android应用
Android Material风格的应用(一)--AppBar TabLayout
Android Material风格的应用(二)--RecyclerView
Android Material风格的应用(三)--DrawerLayout
Android Material风格的应用(四)--FloatActionButton
Android Material风格的应用(五)--CollapsingToolbar
开发环境:
- JDK 8+
- Android Studio 2.2.2
工程源码
Themes And Colors
主题颜色值 res/values/colors.xml
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
如此,添加一个Light主题风格的style res/values/styles.xml
<style name="AppThemeBase" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
使用 parent="Theme.AppCompat.Light.NoActionBar" 是在应用中去除ActionBar运行效果图
布局和动画
添加 Toolbar
在res/layout/activity_main.xml和MainActivity.java中添加toolbar,这里使用CoordinatorLayout作为一个容器
里面会包含TabLayout FloatingActionButton Toolbar等,引入design的支持库compile 'com.android.support:design:24.2.1'activity_main.xml<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_content"> <android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/app_bar_layout"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="@+id/toolbar"
app:layout_scrollFlags="enterAlways|scroll"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"></android.support.v7.widget.Toolbar> <android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabs">
</android.support.design.widget.TabLayout>
</TableLayout>
</android.support.design.widget.AppBarLayout> </android.support.design.widget.CoordinatorLayout>MainActivity.javaToolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);添加TabLayout
TabLayout tabLayout = (TabLayout)findViewById(R.id.tabs);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));添加 Fragment 和 ViewPager
在activity_main.xml中添加ViewPager<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewpager"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</android.support.v4.view.ViewPager>通过添加ViewPager和TabLayout联动,创建3个Fragment的类
ListContentFragment.javaTileContentFragment.java和CardContentFragment.java添加
item_list.xmlitem_tile.xmlitem_card.xml<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List"/>
</LinearLayout>分别创建不同的Fragment文件和布局对应
ListContentFragment.javapublic class ListContentFragment extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.item_list,null);
return v;
}
}TileContentFragment.javapublic class TileContentFragment extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.item_tile,null);
}
}CardContentFragment.javapublic class CardContentFragment extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.item_card,null);
}
}在
MainActivity.java中添加ViewPagerViewPager viewPager = (ViewPager)findViewById(R.id.viewpager);
setupViewPager(viewPager); private void setupViewPager(ViewPager viewPager){
Adapter adapter = new Adapter(getSupportFragmentManager());
adapter.addFragment(new ListContentFragment(),"List");
adapter.addFragment(new TileContentFragment(),"Tile");
adapter.addFragment(new CardContentFragment(),"Card");
viewPager.setAdapter(adapter);
} static class Adapter extends FragmentPagerAdapter{
private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> fragmentTitle = new ArrayList<>();
public Adapter(FragmentManager fm) {
super(fm);
} @Override
public Fragment getItem(int position) {
return fragmentList.get(position);
} @Override
public int getCount() {
return fragmentList.size();
} public void addFragment(Fragment fragment,String title){
fragmentList.add(fragment);
fragmentTitle.add(title);
} @Override
public CharSequence getPageTitle(int position) {
return fragmentTitle.get(position);
}
}
Android Material风格的应用(一)--AppBar TabLayout的更多相关文章
- Android Material风格的应用(五)--CollapsingToolbar
Collapsing Toolbar Android Material风格的应用(一)--AppBar TabLayoutAndroid Material风格的应用(二)--RecyclerViewA ...
- Android Material风格的应用(四)--FloatActionButton
添加 FloatActionButton和SnackBar Android Material风格的应用(一)--AppBar TabLayoutAndroid Material风格的应用(二)--Re ...
- Android Material风格的应用(二)--RecyclerView
添加RecyclerView Android Material风格的应用(一)--AppBar TabLayoutAndroid Material风格的应用(二)--RecyclerViewAndro ...
- Android Material风格的应用(三)--DrawerLayout
添加抽屉导航 Android Material风格的应用(一)--AppBar TabLayoutAndroid Material风格的应用(二)--RecyclerViewAndroid Mater ...
- android ------ AndroidX的 Tablayout(com.google.android.material.tabs.TabLayout) 的使用
前面呢,有写过TabLayout的博客,最近开发用到了AndroidX来解决前面的问题,不要工具类设置下划线的问题了,来总结一下 Android--------TabLayout实现新闻客户端顶部导航 ...
- Android Material Design 兼容库的使用
Android Material Design 兼容库的使用 mecury 前言:近来学习了Android Material Design 兼容库,为了把这个弄懂,才有了这篇博客,这里先推荐两篇博客: ...
- Android Material Design之Toolbar与Palette
转:http://blog.csdn.net/jdsjlzx/article/details/41441083 前言 我们都知道Marterial Design是Google推出的全新UI设计规范,如 ...
- android Material Design详解
原文地址:http://blog.csdn.net/jdsjlzx/article/details/41441083/ 前言 我们都知道Marterial Design是Google推出的全新UI设计 ...
- Android Material Design控件学习(三)——使用TextInputLayout实现酷市场登录效果
前言 前两次,我们学习了 Android Material Design控件学习(一)--TabLayout的用法 Android Material Design控件学习(二)--Navigation ...
随机推荐
- 详解如何在vue项目中引入饿了么elementUI组件
在开发的过程之中,我们也经常会使用到很多组件库:vue 常用ui组件库:https://blog.csdn.net/qq_36538012/article/details/82146649 今天具体说 ...
- jmind-redis一个redis的nio客户端
Redis是一个基于key/value的系统.Redis目前最新版本是2.2.4,用着很不错,不过java版本的客户端比较的不给力,目前redis 客户端jedis 是基于io 的socket . 而 ...
- openssl之EVP系列之5---EVP_Encrypt系列函数具体解释(二)
openssl之EVP系列之5---EVP_Encrypt系列函数详细解释(二) ---依据openssl doc/crypto/EVP_EncryptInit.pod和doc/ssleay.t ...
- Android 经常使用的adb命令
1.安装APK(假设加 -r 參数,保留已设定数据.又一次安装filename.apk) adb install xxx.apk adb install -r xxx.apk 2.卸载APK(假设加 ...
- js---12对象创建方式,构造器,原型
<script type="text/javascript"> var o = {}; var o1 = new Object();//这2种方式创建对象是一样的,因为 ...
- storm单机运行与集群运行问题
使用trident接口时,storm读取kafka数据会将kafka消费记录保存起来,将消费记录的位置保存在tridentTopology.newStream()的第一个参数里, 如果设置成从头开始消 ...
- deep-in-es6(五)
解构 Destructuring: 解构赋值允许使用类似数组或对象字面量的语法将数组和对象的属性赋值给给中变量. 一般情况访问数组中的前三个元素: var first = arr[0]; var se ...
- pwconv---pwunconv 密码投影
pwconv命令用来开启用户的投影密码.Linux系统里的用户和群组密码,分别存放在名称为passwd和group的文件中, 这两个文件位于/etc目录下.因系统运作所需,任何人都得以读取它们,造成安 ...
- 使用C库制作DLL
一.用C编写制作 DLL 如下图所示,是在C++的基础上新建的项目工程: 新建项目的工程文件中有.cpp文件. 由于我们是用C库制作的DLL,显然用C++来编写的是不合适的,我为什么用C库,而不用C+ ...
- JDBC连接池C3P0
连接池 1)传统方式找DriverManager要连接.数目是有限的. 2)传统方式的close().并没有将Connection重用.仅仅是切断应用程序和数据库的桥梁,即无发送到SQL命令到数据库端 ...