美文网首页
12.常用控件

12.常用控件

作者: 为梦想战斗 | 来源:发表于2017-10-19 21:16 被阅读0次

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

相关文章

  • 12.常用控件

    BottomNavigationView、下拉刷新、滑动菜单一、BottomNavigationView在 bui...

  • UI常用控件

    UI常用控件 // // ViewController.m // UI常用控件 // // Created by ...

  • 小工具开发---万能模板

    实现:1)汇总所有控件的常用属性2)汇总每类控件的常用设计3)开发小工具时,复用对应控件 一、所有控件的常用属性 ...

  • Android - Navigation

    Android 基础知识 1. Android 常用控件 2. 控件常用属性 Android 常用知识点 动态权限...

  • IOS开发(三)高级控件

    IOS基础控件思维导图 三、高级控件 1、UITabBarController (1)常用属性 (2)常用方法 (...

  • Flutter常用布局Basic widgets

    Flutter提供了强大的基本控件,我们现在学习最常用的空间。 常用控件一: Text 我们看到这个控件的名字就知...

  • UIDatePicker简述

    // UIDatePicker控件的常用方法 时间选择控件 UIDatePicker *oneDatePicker...

  • IOS开发(二)中级控件

    IOS基础控件思维导图 二、中级控件 1、UIStepper (1)常用属性 (2)常用方法 2、UISlider...

  • Flutter 常用控件及使用技巧

    常用控件 SizeBox:调节 Flutter控件之间的间距控件如: SizeBox(height:8)、Size...

  • 我写了一篇文章《ISO入门之UIView(二)》

    *按钮控件(Button) 按钮控件是最常用的控件之一。iOS中按钮控件Button直接继承于UIControl:...

网友评论

      本文标题:12.常用控件

      本文链接:https://www.haomeiwen.com/subject/ruppuxtx.html