12.常用控件

BottomNavigationView、下拉刷新、滑动菜单
一、BottomNavigationView
在 build.gradle 文件中增加依赖:
compile 'com.android.support:design:25.0.0'

在 res/menu/ 目录下创建一个 xml 文件




    

    

    

    

activity.main:




    

    
    

MainActivity:

mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.bottom_one:
                        mTextView.setText("one");
                        break;
                    case R.id.bottom_two:
                        mTextView.setText("two");
                        break;
                    case R.id.bottom_three:
                        mTextView.setText("three");
                        break;
                    case R.id.bottom_four:
                        mTextView.setText("four");
                        break;
                }
                return true;
            }
        });

二、下拉刷新

 



SwipeRefreshLayout swipeRefresh = (SwipeRefreshLayout) findViewById(R.id.swipe_refesh);
swipeRefresh.setColorSchemeResources(R.color.colorPrimary);
swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(){
                //dosomething
            }
        });
//请求结束时,要停止刷新
swipeRefresh.setRefreshing(false);

三、ScrollView



        

            
            
            
            
            

        

    

四、Bing图片背景



ImageView bingPicImg = (ImageView) findViewById(R.id.bing_pic_img);
        String bingPic = prefs.getString("bing_pic",null);
        if (bingPic != null) {
            Glide.with(this).load(bingPic).into(bingPicImg);
        } else {
            loadBingPic();
        }

private void loadBingPic() {
        String requestBingPic = "http://guolin.tech/api/bing_pic";
        HttpUtil.sendOkHttpRequest(requestBingPic, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String bingPic = response.body().string();
                SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(WeatherActivity.this).edit();
                editor.putString("bing_pic",bingPic);
                editor.apply();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Glide.with(WeatherActivity.this).load(bingPic).into(bingPicImg);
                    }
                });
            }
        });
    }

五、背景和状态栏融合

if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }

android:fitsSystemWindows="true"

六、滑动菜单

标题布局增加一个Button


//布局内容
  
    

drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        navButton = (Button) findViewById(R.id.nav_button);
        navButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                drawerLayout.openDrawer(GravityCompat.START);
            }
        });

关闭方法:activity.drawerLayout.closeDrawers();

你可能感兴趣的:(12.常用控件)