Android SlidingMenu 使用详解(鸿阳)
SlidingMenu GitHub地址:jfeinstein10/SlidingMenu
先从GitHub上下载slidingMenu,打开文件如图:
library是我们依赖的核心内容。打开library文件夹如下:
新建module,并把上面三个红色框住的文件夹复制到新module的src/main中,并把src文件夹名改成java。
//设置侧滑菜单的位置,可选值LEFT , RIGHT , LEFT_RIGHT (两边都有菜单时设置)
menu.setMode(SlidingMenu.LEFT_RIGHT);
// 设置触摸屏幕的模式,可选只MARGIN , CONTENT
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
//根据dimension资源文件的ID来设置阴影的宽度
menu.setShadowWidthRes(R.dimen.shadow_width);
//根据资源文件ID来设置滑动菜单的阴影效果
menu.setShadowDrawable(R.drawable.shadow);
// 这两个都是设置滑动菜单视图的宽度,二选一
//设置SlidingMenu离屏幕的偏移量
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
//设置宽度
menu.setBehindWidth()
// 设置渐入渐出效果的值
menu.setFadeDegree(0.35f);
//设置SlidingMenu与下方视图的移动的速度比,当为1时同时移动,取值0-1
menu.setBehindScrollScale(1.0f);
//设置二级菜单的阴影效果
menu.setSecondaryShadowDrawable(R.drawable.shadow);
//设置右边(二级)侧滑菜单
menu.setSecondaryMenu(R.layout.right_menu_frame);
//为侧滑菜单设置布局
menu.setMenu(R.layout.leftmenu);
//把滑动菜单添加进所有的Activity中,可选值SLIDING_CONTENT , SLIDING_WINDOW
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
package com.example.administrator.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity {
@BindView(R.id.tv)
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
tv.setText("..........................................................");
// configure the SlidingMenu
SlidingMenu menu = new SlidingMenu(this);
menu.setMode(SlidingMenu.LEFT);
// 设置触摸屏幕的模式
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setShadowWidthRes(R.dimen.shadow_width);
menu.setShadowDrawable(R.drawable.shadow);
// 设置滑动菜单视图的宽度
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
// 设置渐入渐出效果的值
menu.setFadeDegree(0.35f);
/**
* SLIDING_WINDOW will include the Title/ActionBar in the content
* section of the SlidingMenu, while SLIDING_CONTENT does not.
*/
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
//为侧滑菜单设置布局
menu.setMenu(R.layout.leftmenu);
}
}
a、继承SlidingActivity
b、然后在onCreate中setBehindContentView(R.layout.leftmenu); 设置侧滑菜单的布局
c、通过getSlidingMenu()得到SlidingMenu对象,然后设置样式
package com.example.administrator.myapplication;
import android.os.Bundle;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingActivity;
/**
* Created by Administrator on 2017/5/11.
*/
public class MAinActivityI extends SlidingActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setBehindContentView(R.layout.leftmenu);
// configure the SlidingMenu
SlidingMenu menu = getSlidingMenu();
menu.setMode(SlidingMenu.LEFT);
// 设置触摸屏幕的模式
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setShadowWidthRes(R.dimen.shadow_width);
menu.setShadowDrawable(R.drawable.shadow);
// 设置滑动菜单视图的宽度
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
// 设置渐入渐出效果的值
menu.setFadeDegree(0.35f);
/**
* SLIDING_WINDOW will include the Title/ActionBar in the content
* section of the SlidingMenu, while SLIDING_CONTENT does not.
*/
// menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
// menu.setMenu(R.layout.leftmenu);
}
}
SlidingMenu的布局中控件的事件都需要写在Activity中,这样代码比较臃肿,一般会使用Fragment作为侧滑菜单的布局容器。
package com.example.administrator.myapplication.demo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.example.administrator.myapplication.R;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
/**
* Created by Administrator on 2017/5/11.
*/
public class MainActivityIII extends AppCompatActivity {
private SlidingMenu slidingMenu;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainiii);
initSlidingMenu();
}
private void initSlidingMenu() {
// configure the SlidingMenu
SlidingMenu menu = new SlidingMenu(this);
//为侧滑菜单设置布局
menu.setMenu(R.layout.menu_left);//左布局
menu.setSecondaryMenu(R.layout.menu_right);//右布局
//替换侧滑菜单的布局为自定义的Fragment
getSupportFragmentManager().beginTransaction().replace(R.id.llLeft, new MenuFragment(menu)).commit();
getSupportFragmentManager().beginTransaction().replace(R.id.llRight, new MenuFragment(menu)).commit();
menu.setBehindOffset(300);// 设置滑动菜单视图的宽度
menu.setMode(SlidingMenu.LEFT_RIGHT);//左右都有侧滑菜单
//滑动菜单可通过滑动屏幕的任意地方被打开
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setShadowWidth(5);//设置阴影宽度
menu.setShadowDrawable(R.drawable.menu_shadow);//设置阴影图片
menu.setSecondaryShadowDrawable(R.drawable.menu_shadow); //引用shape
// 设置渐入渐出效果的值0(没有阴影)到1(全部阴影),
menu.setFadeDegree(0.35f);
//把滑动菜单添加进所在的Activity中
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
}
}
activity_mainiii.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
RelativeLayout>
menu_left.xml
<LinearLayout
android:id="@+id/llLeft"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
LinearLayout>
menu_right.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llRight"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
LinearLayout>
drawable.menu_shadow:阴影
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:endColor="#55000000"
android:startColor="#99000000" />
shape>
fragment
package com.example.administrator.myapplication.demo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import com.example.administrator.myapplication.R;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
/**
* Created by Administrator on 2017/5/11.
*/
public class MenuFragment extends Fragment {
private View view;
private ListView lvMenu;
SlidingMenu slidingMenu;
public MenuFragment(SlidingMenu slidingMenu) {
this.slidingMenu = slidingMenu;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (view == null) {
view = inflater.inflate(R.layout.fragment_menufragment, container, false);
lvMenu = ((ListView) view.findViewById(R.id.lvMenu));
//监听事件
lvMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
Toast.makeText(getActivity(), "you selected " + position, Toast.LENGTH_SHORT).show();
slidingMenu.toggle(); //开关
}
});
}
return view;
}
}
fragment_menufragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/lvMenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:entries="@array/names" />
LinearLayout>
package com.example.administrator.myapplication.fragment;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import com.example.administrator.myapplication.R;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
public class MainActivityII extends SlidingFragmentActivity {
private ViewPager mViewPager;
private FragmentPagerAdapter mAdapter;
private List mFragments = new ArrayList();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_mainii);
// 初始化SlideMenu
initRightMenu();
// 初始化ViewPager
initViewPager();
}
private void initViewPager() {
mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
MainTab01 tab01 = new MainTab01();
MainTab02 tab02 = new MainTab02();
MainTab03 tab03 = new MainTab03();
mFragments.add(tab01);
mFragments.add(tab02);
mFragments.add(tab03);
/**
* 初始化Adapter
*/
mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public int getCount() {
return mFragments.size();
}
@Override
public Fragment getItem(int arg0) {
return mFragments.get(arg0);
}
};
mViewPager.setAdapter(mAdapter);
}
private void initRightMenu() {
//在onCreate中setBehindContentView(R.layout.leftmenu); 设置侧滑菜单的布局
Fragment leftMenuFragment = new MenuLeftFragment();
setBehindContentView(R.layout.left_menu_frame);
getSupportFragmentManager().beginTransaction()
.replace(R.id.id_left_menu_frame, leftMenuFragment).commit();
//通过getSlidingMenu()得到SlidingMenu对象,然后设置样式
SlidingMenu menu = getSlidingMenu();
menu.setMode(SlidingMenu.LEFT_RIGHT);
// 设置触摸屏幕的模式
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
menu.setShadowWidthRes(R.dimen.shadow_width);
menu.setShadowDrawable(R.drawable.shadow);
// 设置滑动菜单视图的宽度
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
// menu.setBehindWidth()
// 设置渐入渐出效果的值
menu.setFadeDegree(0.35f);
// menu.setBehindScrollScale(1.0f);
menu.setSecondaryShadowDrawable(R.drawable.shadow);
//设置右边(二级)侧滑菜单
menu.setSecondaryMenu(R.layout.right_menu_frame);
Fragment rightMenuFragment = new MenuRightFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.id_right_menu_frame, rightMenuFragment).commit();
}
public void showLeftMenu(View view) {
getSlidingMenu().showMenu();
}
public void showRightMenu(View view) {
getSlidingMenu().showSecondaryMenu();
}
}
package com.example.administrator.myapplication.fragment;
import java.util.Arrays;
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import com.example.administrator.myapplication.R;
public class MenuLeftFragment extends Fragment {
private View mView;
private ListView mCategories;
private List mDatas = Arrays.asList("聊天", "发现", "通讯录", "朋友圈", "订阅号");
private ListAdapter mAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (mView == null) {
initView(inflater, container);
}
return mView;
}
private void initView(LayoutInflater inflater, ViewGroup container) {
mView = inflater.inflate(R.layout.left_menu, container, false);
mCategories = (ListView) mView.findViewById(R.id.id_listview_categories);
mAdapter = new ArrayAdapter(getActivity(),
android.R.layout.simple_list_item_1, mDatas);
mCategories.setAdapter(mAdapter);
}
}
package com.example.administrator.myapplication.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.administrator.myapplication.R;
public class MenuRightFragment extends Fragment {
private View mView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (mView == null) {
mView = inflater.inflate(R.layout.right_menu, container, false);
}
return mView;
}
}
主界面布局:activity_mainii.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="@drawable/title_bar">
<ImageButton
android:id="@+id/id_iv_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="3dp"
android:background="@drawable/showright_selector"
android:onClick="showLeftMenu" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="微信"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold" />
<ImageButton
android:id="@+id/id_iv_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="3dp"
android:background="@drawable/showleft_selector"
android:onClick="showRightMenu" />
RelativeLayout>
<android.support.v4.view.ViewPager
android:id="@+id/id_viewpager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
LinearLayout>
左侧菜单fragment的布局:left_menu.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="@drawable/title_bar">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="功能"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold" />
LinearLayout>
<ListView
android:id="@+id/id_listview_categories"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAlignment="center" />
LinearLayout>
右侧菜单fragment的布局:right_menu.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eee"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="45dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="设置"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold" />
LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:clickable="true"
android:drawableRight="@drawable/mm_submenu_normal"
android:gravity="center_vertical"
android:paddingLeft="25dp"
android:text="个人信息"
android:textColor="#000"
android:textSize="16sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:clickable="true"
android:drawableRight="@drawable/mm_submenu_normal"
android:gravity="center_vertical"
android:paddingLeft="25dp"
android:text="二维码名片"
android:textColor="#000"
android:textSize="16sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:clickable="true"
android:drawableRight="@drawable/mm_submenu_normal"
android:gravity="center_vertical"
android:paddingLeft="25dp"
android:text="腾讯微博"
android:textColor="#000"
android:textSize="16sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:clickable="true"
android:drawableRight="@drawable/mm_submenu_normal"
android:gravity="center_vertical"
android:paddingLeft="25dp"
android:text="添加朋友"
android:textColor="#000"
android:textSize="16sp" />
LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:clickable="true"
android:drawableRight="@drawable/mm_submenu_normal"
android:gravity="center_vertical"
android:paddingLeft="25dp"
android:text="我的帐号"
android:textColor="#000"
android:textSize="16sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:clickable="true"
android:drawableRight="@drawable/mm_submenu_normal"
android:gravity="center_vertical"
android:paddingLeft="25dp"
android:text="信鸿洋得永生"
android:textColor="#000"
android:textSize="16sp" />
LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:clickable="true"
android:drawableRight="@drawable/mm_submenu_normal"
android:gravity="center_vertical"
android:paddingLeft="25dp"
android:text="聊天背景"
android:textColor="#000"
android:textSize="16sp" />
LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:clickable="true"
android:drawableRight="@drawable/mm_submenu_normal"
android:gravity="center_vertical"
android:paddingLeft="25dp"
android:text="通用"
android:textColor="#000"
android:textSize="16sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:clickable="true"
android:drawableRight="@drawable/mm_submenu_normal"
android:gravity="center_vertical"
android:paddingLeft="25dp"
android:text="插件"
android:textColor="#000"
android:textSize="16sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:clickable="true"
android:drawableRight="@drawable/mm_submenu_normal"
android:gravity="center_vertical"
android:paddingLeft="25dp"
android:text="黑名单"
android:textColor="#000"
android:textSize="16sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:clickable="true"
android:drawableRight="@drawable/mm_submenu_normal"
android:gravity="center_vertical"
android:paddingLeft="25dp"
android:text="隐私"
android:textColor="#000"
android:textSize="16sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:clickable="true"
android:drawableRight="@drawable/mm_submenu_normal"
android:gravity="center_vertical"
android:paddingLeft="25dp"
android:text="系统通知"
android:textColor="#000"
android:textSize="16sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:clickable="true"
android:drawableRight="@drawable/mm_submenu_normal"
android:gravity="center_vertical"
android:paddingLeft="25dp"
android:text="流量同级"
android:textColor="#000"
android:textSize="16sp" />
LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:clickable="true"
android:drawableRight="@drawable/mm_submenu_normal"
android:gravity="center_vertical"
android:paddingLeft="25dp"
android:text="帮助与反馈"
android:textColor="#000"
android:textSize="16sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:clickable="true"
android:drawableRight="@drawable/mm_submenu_normal"
android:gravity="center_vertical"
android:paddingLeft="25dp"
android:text="关于微信"
android:textColor="#000"
android:textSize="16sp" />
LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:clickable="true"
android:drawableRight="@drawable/mm_submenu_normal"
android:gravity="center_vertical"
android:paddingLeft="25dp"
android:text="清空聊天记录"
android:textColor="#000"
android:textSize="16sp" />
LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:orientation="vertical">
<Button
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_marginLeft="20sp"
android:layout_marginRight="20sp"
android:background="@drawable/exit_btn"
android:gravity="center"
android:text="退出登录"
android:textColor="#fff"
android:textSize="16sp" />
LinearLayout>
LinearLayout>
ScrollView>
LinearLayout>
左侧菜单的容器:left_menu_frame.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/id_left_menu_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
右侧菜单的容器:right_menu_frame.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/id_right_menu_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
viewpager的第一页:
package com.example.administrator.myapplication.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.administrator.myapplication.R;
public class MainTab01 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.main_tab_01, container, false);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ly_main_weixin"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fcfcfc"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="this is first tab !"
android:textColor="#000000"
android:textSize="30sp" />
LinearLayout>
viewpager的第二页:
package com.example.administrator.myapplication.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.administrator.myapplication.R;
public class MainTab02 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View messageLayout = inflater.inflate(R.layout.main_tab_02, container, false);
return messageLayout;
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ly_main_weixin"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fcfcfc"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="this is second tab !"
android:textColor="#000000"
android:textSize="30sp" />
LinearLayout>
viewpager的第三页:
package com.example.administrator.myapplication.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.administrator.myapplication.R;
public class MainTab03 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View newsLayout = inflater.inflate(R.layout.main_tab_03, container, false);
return newsLayout;
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ly_main_weixin"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fcfcfc"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="this is third tab !"
android:textColor="#000000"
android:textSize="30sp" />
LinearLayout>