Android Material风格的应用(一)--AppBar TabLayout
来源:互联网 发布:sql 链接查询结果 编辑:程序博客网 时间:2024/06/05 20:54
打造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.CoordinatorLayoutxmlns: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> <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tabs"> </TableLayout></android.support.design.widget.AppBarLayout></android.support.design.widget.CoordinatorLayout>
MainActivity.java
Toolbar 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.ViewPagerandroid: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.java
TileContentFragment.java
和CardContentFragment.java
添加
item_list.xml
item_tile.xml
item_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.java
public class ListContentFragment extends Fragment{@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View v = inflater.inflate(R.layout.item_list,null); return v;}}
TileContentFragment.java
public class TileContentFragment extends Fragment{@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.item_tile,null);}}
CardContentFragment.java
public class CardContentFragment extends Fragment{@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.item_card,null);}}
在
MainActivity.java
中添加ViewPager
ViewPager 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风格的应用(二)--RecyclerView
- Android Material风格的应用(三)--DrawerLayout
- Android Material风格的应用(四)--FloatActionButton
- Android Material风格的应用(五)--CollapsingToolbar
- 创建Material Design风格的Android应用--应用主题
- 创建Material Design风格的Android应用--应用主题
- 创建Material Design风格的Android应用--应用主题
- 创建Material Design风格的Android应用--应用主题
- 创建Material Design风格的Android应用--应用主题
- Material Design(一):TabLayout的使用
- 创建Material Design风格的Android应用--使用Drawable
- 创建Material Design风格的Android应用--使用自定义动画
- 创建Material Design风格的Android应用--使用自定义动画
- 创建Material Design风格的Android应用--使用Drawable
- 创建Material Design风格的Android应用--使用自定义动画
- 创建Android Material Design的应用主题风格
- 创建Material Design风格的Android应用--使用自定义动画
- svn常用命令
- [Python] Python基础笔记
- iOS 获取当前设备具体型号(当前支持到iPhone7/iPhone7P )、系统版本、电量、序列号、设备名称、应用相关信息获取等
- solution of POJ:3190.Stall Reservations
- 机械设备用油液监测方法
- Android Material风格的应用(一)--AppBar TabLayout
- 二叉搜索树的实现和前序遍历
- sass_混合声明和调用
- WSO2 ESB(1)
- Android Material风格的应用(二)--RecyclerView
- 优雅编码框架简介一
- eclipse32位和64位的区别
- Android Material风格的应用(三)--DrawerLayout
- Fiddler2抓包中文乱码解决方法