【原创】利用AppBarLayout的功能使用ViewPager在界面中插入另一个activity界面

首先,先看一下运行的结果(如图)

在这里插入图片描述

1.创建第一个activity(即需要插入activity的总界面)

在这里插入图片描述

需要注意的是这个activity的布局不是平时用的相对布局或者线性布局,而是AppBarLayout
<android.support.design.widget.AppBarLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FEFEFE">
    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:background="@drawable/backgroundcolor"
        android:text="日常账本"
        android:textSize="25sp"
        android:textColor="#FFFFFF"
        android:gravity="center"/>

    <RelativeLayout
        android:id="@+id/rl_1"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_below="@id/tv_title"
        android:background="#FEFEFE">
        <TextView
            android:id="@+id/tv_month"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="本月"
            android:textSize="20sp"
            android:textColor="#4682B4"
            android:textStyle="bold"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"/>
        <Button
            android:id="@+id/btn_zuo"
            android:layout_width="30dp"
            android:layout_height="35dp"
            android:background="@drawable/shangyibu"
            android:layout_alignParentLeft="true"
            android:layout_marginTop="5dp"/>
        <Button
            android:id="@+id/btn_you"
            android:layout_width="30dp"
            android:layout_height="35dp"
            android:background="@drawable/xiayibu"
            android:layout_alignParentRight="true"
            android:layout_marginTop="5dp"/>
        <TextView
            android:id="@+id/tv_zhichu"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_toRightOf="@id/btn_zuo"
            android:layout_below="@id/btn_zuo"
            android:paddingLeft="30dp"
            android:text="支出"
            android:textColor="#ABABAB"
            android:textSize="16sp"/>
        <TextView
            android:id="@+id/tv_shouru"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:paddingLeft="30dp"
            android:textSize="16sp"
            android:textColor="#ABABAB"
            android:layout_toRightOf="@id/tv_month"
            android:layout_below="@id/btn_you"
            android:text="收入"/>
        
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想在 `ViewPager` 添加一个按钮,用于在最后一页跳转到主界面,可以采取以下步骤: 1. 在你的 `ViewPager` ,添加一个额外的页面,用于显示最后一页,该页面应该包含你的进入主界面按钮。 2. 创建一个自定义适配器类,继承自 `PagerAdapter`,并重写以下方法: - `getCount()` 方法,用于返回页面数量。在这个方法,你需要返回 `ViewPager` 页面的数量加 1,这个额外的页面用于显示最后一页。 - `instantiateItem()` 方法,用于创建页面。在这个方法,你需要检查当前位置是否是最后一页,如果是,就创建你的最后一页,否则就创建普通的页面。 3. 在你的最后一页,添加一个进入主界面的按钮,并在点击事件执行跳转主界面逻辑。 下面是一个示例代码: ```java public class MyPagerAdapter extends PagerAdapter { private Context mContext; public MyPagerAdapter(Context context) { mContext = context; } @Override public int getCount() { // 返回页面数量加 1,用于显示最后一页 return 3 + 1; } @Override public Object instantiateItem(ViewGroup container, int position) { LayoutInflater inflater = LayoutInflater.from(mContext); View view; if (position == 3) { // 如果是最后一页,创建包含进入主界面按钮的页面 view = inflater.inflate(R.layout.last_page_layout, container, false); Button enterButton = view.findViewById(R.id.enter_button); enterButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 执行跳转主界面逻辑 Intent intent = new Intent(mContext, MainActivity.class); mContext.startActivity(intent); } }); } else { // 如果是普通页面,创建普通页面 view = inflater.inflate(R.layout.normal_page_layout, container, false); // 设置页面内容 } container.addView(view); return view; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } } ``` 在上面的代码,`R.layout.last_page_layout` 应该是你创建的包含进入主界面按钮的布局文件,`R.id.enter_button` 应该是你的进入主界面按钮的资源 ID。你需要将这两个值替换为你自己的值。注意,在点击事件,你需要将 `MainActivity.class` 替换为你自己的主界面类。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值