第三十五篇-AppBarLayout的使用

效果图:

添加appbarlayout到xml文件中,然后在toolbar下面添加一个imageview并设置居中放置,我放置的是上面那个安卓的图标。

根据之前学过的toolbar那一节,结合viewpaper和toolbar设置三个页面,这时,运行程序,可能发现那三个页面并没有显示出来,NetedScollView这是个可滚动的页面,单击它,在右侧勾选fillViewport。在运行程序就可以显示页面了。

page1

page2

page3

附上代码

main.java

package com.example.aimee.appbarlayouttest;

import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    TabLayout tabLayout;
    ViewPager viewPager;
    List<Fragment>fragments;
    String[] title={"新闻","财经","娱乐"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tabLayout=findViewById(R.id.tabs);
        viewPager=findViewById(R.id.viewpaper);

        fragments=new ArrayList<>();
        fragments.add(new MyFragment1());
        fragments.add(new MyFragment2());
        fragments.add(new MyFragment3());
        MyAdpter myAdpter=new MyAdpter(getSupportFragmentManager(),fragments);
        viewPager.setAdapter(myAdpter);
        tabLayout.setupWithViewPager(viewPager);
    }

    private class MyAdpter extends FragmentPagerAdapter{
        private List<Fragment>fragments;

        public MyAdpter(FragmentManager fm,List<Fragment>fragments) {
            super(fm);
            this.fragments=fragments;
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return title[position];
        }
    }
}

MyFragment1.java

package com.example.aimee.appbarlayouttest;

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

public class MyFragment1 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view1=inflater.inflate(R.layout.layout_page1,container,false);
        return view1;
    }
}

MyFragment2.java

package com.example.aimee.appbarlayouttest;

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

public class MyFragment2 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view1=inflater.inflate(R.layout.layout_page2,container,false);
        return view1;
    }
}

MyFragment3.java

package com.example.aimee.appbarlayouttest;

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

public class MyFragment3 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view1=inflater.inflate(R.layout.layout_page3,container,false);
        return view1;
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">

        <android.support.v7.widget.Toolbar
            android:gravity="center"
            android:layout_height="?attr/actionBarSize"
            android:layout_width="match_parent"
            app:layout_scrollFlags="scroll|enterAlways">
            <ImageView
                android:src="@mipmap/ic_launcher"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|enterAlways"
            app:tabMode="scrollable">

            <android.support.design.widget.TabItem
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Tab1" />

            <android.support.design.widget.TabItem
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Tab2" />

            <android.support.design.widget.TabItem
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Tab3" />
        </android.support.design.widget.TabLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity">

            <android.support.v4.view.ViewPager
                android:id="@+id/viewpaper"
                android:layout_width="match_parent"
                android:layout_height="0dp" />
        </android.support.constraint.ConstraintLayout>
    </android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>

layout_page1.xml

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

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
</LinearLayout>

layout_page2.xml

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

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>

layout_page3.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"
    android:layout_width="match_parent"
    android:gravity="center"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <ImageView
                android:scaleType="fitXY"
                android:id="@+id/imageView3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/a3" />

            <ImageView
                android:scaleType="fitXY"
                android:id="@+id/imageView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/a1" />

            <ImageView
                android:scaleType="fitXY"
                android:id="@+id/imageView7"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/a2" />

            <ImageView
                android:scaleType="fitXY"
                android:id="@+id/imageView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/a5" />

            <ImageView
                android:scaleType="fitXY"
                android:id="@+id/imageView6"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/a4" />
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

</LinearLayout>

注:图片放在drawable下面。

原文地址:https://www.cnblogs.com/smart-zihan/p/9961583.html

时间: 2024-08-29 22:59:53

第三十五篇-AppBarLayout的使用的相关文章

mysql 第三十五篇文章~xtarbackup增量备份以及策略

一 简介: 今天咱们来探讨下增量备份的策略 二 背景: 随着数据量的日益增长,全备已经不现实了,所以探讨并测试了增量备份策略 三 具体策略: 1  一周为界限,一天做全备,其他时间以全备为基础进行增量备份 2  一周为界限,进行打包,然后上传到备份服务器 四 具体脚本: 第一部分 参数变量初始化 #!/bin/bash #config xingqi=`date +%w` DATE=`date +%Y%m%d%H` DATEyst=`date +"%Y%m%d%H" -d "-

第三十五篇 os模块、sys模块、json模块、pickle模块

目录 一.os模块 二.sys模块 三.json模块 dump和load 四.pickle模块 一.os模块 os模块和操作系统交互,主要用于文件操作 import os # test.py文件中 # os.mkdir() 一个路径参数和一个字符串参数.如果有路径参数,则在该路径下创建一个新的文件夹:如果无路径参数,则在当前文件的同级路径下创建一个新的文件夹 os.mkdir('king') # 创建了和test.py文件同级目录下的名为king的文件夹 # os.removedirs() 一个

小刘同学的第一百三十五篇日记

今天大致把论文改完了. 还剩一章的内容,明早大概2个小时就能改完整片论文了. 今天收到录用通知了-- 感觉很殇--可能要去小县城上班去了. 而且也不是做技术-- 不知道后面的路会怎么样, 牢记使命,不忘初心吧( ̄▽ ̄)~* 大家早安~ morning~ 原文地址:https://www.cnblogs.com/xiaoliutongxue/p/9022457.html

我的第三十五篇博客---flask-wtf表单验证

在Flask中,为了处理web表单,我们可以使用Flask-WTF扩展,它封装了WTForms,并且它有验证表单数据的功能WTForms支持的HTML标准字段 字段对象 说明StringField 文本字段TextAreaField 多行文本字段PasswordField 密码文本字段HiddenField 隐藏文件字段DateField 文本字段,值为datetime.date文本格式DateTimeField 文本字段,值为datetime.datetime文本格式IntegerField

C++第三十五篇 -- 写第一个驱动开发程序

VS2017+WDK+VMware12+Win10环境配置完毕,接下来写第一个驱动程序. 1.新建一个KMDF的程序. 2.配置项目属性. 3.编译项目.一般这里应该成功,我一台电脑成功了,另一台电脑失败了.两台电脑OS的版本不同,一个1803(失败),一个1809(成功).失败的信息如下: 做法: 好像不管用,上google搜索了下,修改平台集V141,也不管用.最后,看到一个说安装WDK1809的.PS:有问题的是VS2017+WDK1803.VS2017+WDK1809是正常的.不过正常编

秒杀多线程第十五篇 关键段,事件,互斥量,信号量的“遗弃”问题

版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 秒杀多线程第十五篇 关键段,事件,互斥量,信号量的“遗弃”问题 在<秒杀多线程第九篇 经典线程同步总结 关键段 事件 互斥量 信号量>中对经典多线程同步互斥问题进行了回顾和总结,这篇文章对Windows系统下常用的线程同步互斥机制——关键段.事件.互斥量.信号量进行了总结.有网友问到互斥量能处理“遗弃”问题,事件和信号量是否也能处理“遗弃”问题.因此本文将对事件和信号量作个试验,看看事件和信号量能否处理“遗弃”问题. 一.

第十五篇 Integration Services:SSIS参数

本篇文章是Integration Services系列的第十五篇,详细内容请参考原文. 简介在前一篇,我们使用SSDT-BI将第一个SSIS项目My_First_SSIS_Project升级/转换到SSIS 2012.在这一篇,我们将探讨SSIS变量的姊妹:SSIS参数.我们将展示参数配置,通过包参数管理动态属性值,以及在SSIS包执行期间参数是如何配置和使用的.首先在SSDT-BI打开转换过的My_First_SSIS_Project,如图15.1所示:图15.1My_First_SSIS_P

NeHe OpenGL教程 第三十五课:播放AVI

转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线教程的编写,以及yarn的翻译整理表示感谢. NeHe OpenGL第三十五课:播放AVI 在OpenGL中播放AVI: 在OpenGL中如何播放AVI呢?利用Windows的API把每一帧作为纹理绑定到OpenGL中,虽然很慢,但它的效果不错.你可以试试. 首先我得说我非常喜欢这一章节.Jonat

Egret入门学习日记 --- 第二十五篇(书中 9.16~9.17 节 内容)

第二十五篇(书中 9.16~9.17 节 内容) 对于昨天的关于 List 组件使用的问题,我打算到书中提到List之后,再回头补充. 还有就是 Scroller 的 TileLayout 布局方式,也要去研究一下. 好了,开始按照书中内容一步一步走. 开始 9.16节. 重点: 1.设定TabBar皮肤. 2.设置TabBar布局. 操作: 1.设定TabBar皮肤. 第一步,准备素材! 第二步,创建 exml 文件! 第三步,拖入组件!约束大小! 第四步,增加两个状态 down 和 up.