12.常用控件

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

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

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/bottom_one"
          android:icon="@drawable/at"
          android:title="one" />

    <item android:id="@+id/bottom_two"
          android:icon="@drawable/at"
          android:title="two" />

    <item android:id="@+id/bottom_three"
          android:icon="@drawable/at"
          android:title="three" />

    <item android:id="@+id/bottom_four"
          android:icon="@drawable/at"
          android:title="four" />
</menu>

activity.main:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    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"
    tools:context="com.lewanjiang.buttonnav1.MainActivity">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/botton_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:menu="@menu/menu_botton_nav"
        app:itemBackground="@color/colorPrimary"
        app:itemIconTint="#fff"
        app:itemTextColor="#fff" />
    
</RelativeLayout>

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;
            }
        });

二、下拉刷新

 <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_refesh"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
</android.support.v4.widget.SwipeRefreshLayout>


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

<ScrollView
        android:id="@+id/weather_layout"
        android:scrollbars="none"
        android:overScrollMode="never"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

            <include layout="@layout/title" />
            <include layout="@layout/now" />
            <include layout="@layout/forecast" />
            <include layout="@layout/aqi" />
            <include layout="@layout/suggestion" />

        </LinearLayout>

    </ScrollView>

四、Bing图片背景

<ImageView
        android:id="@+id/bing_pic_img"
        android:scaleType="centerCrop"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

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

<android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_height="match_parent"
        android:layout_width="match_parent" >
//布局内容
  <fragment
            android:id="@+id/choose"
            android:name="com.lewanjiang.weather.ChooseFragment"
            android:layout_gravity="start"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.v4.widget.DrawerLayout>

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();

推荐阅读更多精彩内容