例:
依赖:
dependencies { //侧拉SlidingMenu implementation project(':SlidingMenu-master') //BottomTabBar implementation 'com.hjm:BottomTabBar:1.1.2' implementation 'com.android.support.test:runner:1.0.2' //tablayou implementation 'com.android.support:design:26+' //banner implementation 'com.youth.banner:banner:1.4.9' //ok implementation 'com.squareup.okhttp3:okhttp:3.6.0' //gson implementation 'com.google.code.gson:gson:2.8.2' //frcon compile 'com.facebook.fresco:fresco:0.14.1' //Glide: implementation 'com.github.bumptech.glide:glide:3.7.0' //xrecyclerview compile 'com.jcodecraeer:xrecyclerview:1.3.2' }//侧拉SlidingMenu
implementation project(':SlidingMenu-master')
需要导入一个自定义工具jar包:点击打开链接
这里注意一下:把它改成16(因为版本的原因,不然会报错)
minSdkVersion 16
MainActivity
package com.example.day_0705_yikezhong; import android.animation.Animator; import android.animation.ArgbEvaluator; import android.animation.ObjectAnimator; import android.content.Context; import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.view.WindowManager; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.Switch; import android.widget.TextView; import com.hjm.bottomtabbar.BottomTabBar; import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu; public class MainActivity extends AppCompatActivity { /** * 日间模式 */ private final static int DAY_THEME = 1; /** * 夜间模式 */ private final static int NIGHT_THEME = 2; private int statusBarHeight; private TextView toutiaomu; private SlidingMenu menu; private boolean shu; private int width; private int height; private RelativeLayout rootLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rootLayout = findViewById(R.id.relat); //获取高度 WindowManager wm = this.getWindowManager(); width = wm.getDefaultDisplay().getWidth(); height = wm.getDefaultDisplay().getHeight(); int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) { //根据资源ID获取响应的尺寸值 statusBarHeight = getResources().getDimensionPixelSize(resourceId); } initSlidingMenu(); BottomTabBar bar = findViewById(R.id.bottom_tab_bar); toutiaomu = findViewById(R.id.toutiaomu); bar.init(getSupportFragmentManager()) .addTabItem("推荐",R.drawable.raw_1500085367,R.drawable.raw_1500083878,RecommendFragment.class) .addTabItem("段子",R.drawable.raw_1500085899,R.drawable.raw_1500085327,ParagraphFragment.class) .addTabItem("视频",R.drawable.raw_1500086067,R.drawable.raw_1500083686,VideoFragment.class); bar.setOnTabChangeListener(new BottomTabBar.OnTabChangeListener() { @Override public void onTabChange(int position, String name, View view) { toutiaomu.setText(name); } }); ImageView imageview = findViewById(R.id.main_imageview); imageview.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { menu.toggle(); } }); } private void initSlidingMenu() { // 设置SlidingMenu menu = new SlidingMenu(this); //设置侧滑方式为Left:左侧 RIGHT:右侧 LEFT_RIGHT:两侧都可以 menu.setMode(SlidingMenu.LEFT); /** * 设置拖拽模式 * SlidingMenu.TOUCHMODE_FULLSCREEN:全屏触摸有效 * SlidingMenu.TOUCHMODE_MARGIN:拖拽边缘有效 * SlidingMenu.TOUCHMODE_NONE:不响应拖拽事件 */ menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE); //设置阴影的宽度 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. * 将侧滑菜单添加到Activity中, * SLIDING_CONTENT作为内容显示 * SLIDING_WINDOW作为窗口显示 */ menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT); //为侧滑菜单设置布局 View inflate = View.inflate(MainActivity.this, R.layout.leftmenu, null); menu.setMenu(inflate); Switch aSwitch = inflate.findViewById(R.id.switch01); initView(); aSwitch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { switchDayNightTheme(); } }); } private void initView() { //加载当前的主题 if (getCurrentTheme() == DAY_THEME) { // setMyTheme(DAY_THEME); setDayThemeInfo(); } else if (getCurrentTheme() == NIGHT_THEME) { // setMyTheme(NIGHT_THEME); setNightThemeInfo(); } else { // setMyTheme(DAY_THEME); setDayThemeInfo(); } } /** * 设置模式 * * @param dayTheme */ private void setMyTheme(int dayTheme) { switch (dayTheme) { case DAY_THEME: setDayTheme(); break; case NIGHT_THEME: setNightTheme(); break; default: setDayTheme(); break; } } /** * 设置夜间模式 */ private void setNightTheme() { // final ImageView imageView = new ImageView(this); // imageView.setLayoutParams(new ViewGroup.LayoutParams(width, height-statusBarHeight)); // Bitmap bitmap = loadBitmapFromView(rootLayout); // imageView.setImageBitmap(bitmap); // rootLayout.addView(imageView); //设置新主题 setNightThemeInfo(); int colorA = Color.parseColor("#ffffff"); int colorB = Color.parseColor("#333444"); ObjectAnimator objectAnimator = ObjectAnimator.ofInt(rootLayout, "backgroundColor", colorA, colorB); objectAnimator.setDuration(800); objectAnimator.setEvaluator(new ArgbEvaluator()); objectAnimator.addListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) { } @Override public void onAnimationEnd(Animator animation) { // rootLayout.removeView(imageView); } @Override public void onAnimationCancel(Animator animation) { } @Override public void onAnimationRepeat(Animator animation) { } }); objectAnimator.start(); } boolean flag = true; /** * 设置夜间模式具体代码 */ private void setNightThemeInfo() { rootLayout.setBackgroundColor(Color.parseColor("#333444")); // tvColor.setTextColor(Color.parseColor("#666666")); //imageView.setImageResource(R.mipmap.night_icon); /*ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0f, 1f); animator.setDuration(1200); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float currentValue = (float) animation.getAnimatedValue(); int value = (int) (currentValue*100); Log.e("zhihu", "onAnimationUpdate: "+value); if(value < 5 && flag) { imageView.setImageResource(R.mipmap.night_icon); flag = !flag; } } }); animator.start();*/ } /** * 设置日渐模式具体代码 */ private void setDayThemeInfo() { rootLayout.setBackgroundColor(Color.parseColor("#FFFFFF")); // tvColor.setTextColor(Color.parseColor("#222222")); //imageView.setImageResource(R.mipmap.day_icom); } /** * 设置日间模式 */ private void setDayTheme() { // final ImageView imageView = new ImageView(this); // imageView.setLayoutParams(new ViewGroup.LayoutParams(width, height-statusBarHeight)); // imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); // Bitmap bitmap = loadBitmapFromView(rootLayout); // imageView.setImageBitmap(bitmap); // rootLayout.addView(imageView); //设置新主题 setDayThemeInfo(); int colorA = Color.parseColor("#333444"); int colorB = Color.parseColor("#ffffff"); ObjectAnimator objectAnimator = ObjectAnimator.ofInt(rootLayout, "backgroundColor", colorA, colorB); objectAnimator.setDuration(800); objectAnimator.setEvaluator(new ArgbEvaluator()); objectAnimator.addListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) { } @Override public void onAnimationEnd(Animator animation) { // rootLayout.removeView(imageView); } @Override public void onAnimationCancel(Animator animation) { } @Override public void onAnimationRepeat(Animator animation) { } }); objectAnimator.start(); } /** * 测试方法 * @param imageView */ private void testDay(final ImageView imageView) { //设置imageView渐隐动画 AlphaAnimation animation = new AlphaAnimation(1.0f, 0.0f); animation.setDuration(2000); animation.setFillAfter(true); animation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { Log.e("zhihu", "zhihu -- onAnimationStart"); } @Override public void onAnimationEnd(Animation animation) { Log.e("zhihu", "zhihu -- onAnimationEnd"); rootLayout.removeView(imageView); } @Override public void onAnimationRepeat(Animation animation) { } }); imageView.startAnimation(animation); } /** * 获取view截图对应的bitmap * @param v * @return */ public Bitmap loadBitmapFromView(View v) { Bitmap b = Bitmap.createBitmap(width, height-statusBarHeight, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b); v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height); v.draw(c); return b; } /** * 切换日渐模式或夜间模式 */ private void switchDayNightTheme() { int curMode = getCurrentTheme(); switch (curMode) { case DAY_THEME: saveCurrentTheme(NIGHT_THEME); setMyTheme(NIGHT_THEME); break; case NIGHT_THEME: saveCurrentTheme(DAY_THEME); setMyTheme(DAY_THEME); break; default: saveCurrentTheme(DAY_THEME); setMyTheme(DAY_THEME); break; } } /** * 保存当前模式 * * @param mode */ private void saveCurrentTheme(int mode) { SharedPreferences preferences = getSharedPreferences("AppTheme", Context.MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); editor.putInt("mode", mode); editor.apply(); } /** * 获取当前模式 * * @m mode */ private int getCurrentTheme() { SharedPreferences preferences = getSharedPreferences("AppTheme", Context.MODE_PRIVATE); int currentMode = preferences.getInt("mode", 0); return currentMode; } }activity_main
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" tools:context=".MainActivity" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:hjm="http://schemas.android.com/apk/res-auto" android:id="@+id/relat"> <com.hjm.bottomtabbar.BottomTabBar android:id="@+id/bottom_tab_bar" android:layout_width="match_parent" android:layout_height="match_parent" hjm:tab_divider_background="#FF0000" hjm:tab_divider_height="5dp" hjm:tab_font_size="6sp" hjm:tab_img_font_padding="0dp" hjm:tab_img_height="30dp" hjm:tab_img_width="30dp" hjm:tab_isshow_divider="true" hjm:tab_padding_bottom="5dp" hjm:tab_padding_top="8dp" hjm:tab_selected_color="@color/colorPrimary" hjm:tab_unselected_color="#000000" android:layout_below="@+id/relamain" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal" android:id="@+id/relamain" android:background="@color/colorPrimary"> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:src="@drawable/ic_launcher_background" android:paddingLeft="10dp" android:id="@+id/main_imageview" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="推荐" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:textSize="30dp" android:id="@+id/toutiaomu" /> <ImageView android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/raw_1500034665" android:layout_alignParentRight="true" /> </RelativeLayout> </RelativeLayout>//设置阴影的宽度
menu.setShadowWidthRes(R.dimen.shadow_width);
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- Default screen margins, per the Android Design guidelines. --> <dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen> <dimen name="shadow_width">16dp</dimen> <dimen name="slidingmenu_offset">200dp</dimen> </resources>//设置阴影的图片
menu.setShadowDrawable(R.drawable.shadow);
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <gradient android:endColor="#33000000" android:centerColor="#11000000" android:startColor="#00000000" /> </selector>//为侧滑菜单设置布局
View inflate = View.inflate(MainActivity.this, R.layout.leftmenu, null);
menu.setMenu(inflate);
<?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" android:id="@+id/lin"> <Switch android:id="@+id/switch01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="夜间模式" /> </LinearLayout>
toutiaomu = findViewById(R.id.toutiaomu);
bar.init(getSupportFragmentManager())
.addTabItem("推荐",R.drawable.raw_1500085367,R.drawable.raw_1500083878,RecommendFragment.class)
.addTabItem("段子",R.drawable.raw_1500085899,R.drawable.raw_1500085327,ParagraphFragment.class)
.addTabItem("视频",R.drawable.raw_1500086067,R.drawable.raw_1500083686,VideoFragment.class);
raw_1500034665.png
raw_1500083686.png
raw_1500083878.png
raw_1500085327.png
raw_1500085367.png
raw_1500085899.png
public class ParagraphFragment extends Fragment { private View view; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragmentparagraph, container, false); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); inte(); } private void inte() { } }