Material Design的一些常用控件


废话不多说直接先贴代码

package com.example.lsn2_materialdesign_theme_style;

import android.content.DialogInterface;
import android.graphics.Color;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.ListPopupWindow;
import android.support.v7.widget.PopupMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    ProgressBar progressBr;
    SwipeRefreshLayout srl;
    AppCompatButton button1 ;
    Button button2 ;
    boolean flag = true;
    int progress = 0;
    private ArrayAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //对话框
        Button showAlertDialog = (Button) findViewById(R.id.showDialog);
        showAlertDialog.setOnClickListener(this);

        //进度条
        progressBr = (ProgressBar) findViewById(R.id.prograssBar);
        progressBr.setMax(100);
        progressBr.setProgress(0);

        //下拉刷新
        srl = (SwipeRefreshLayout) findViewById(R.id.srl);
        srl.setSize(SwipeRefreshLayout.LARGE);
        //设置下拉刷新的监听
        srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                srl.setBackgroundColor(Color.WHITE);
                while(flag){
                    progress += 10;
                    progressBr.setProgress(progress);
                    if(progress >= 100){
                        flag = false ;
                    }
                }
                srl.setRefreshing(false);
            }
        });
        //设置颜色的模式
        srl.setColorSchemeColors(Color.RED, Color.BLUE, Color.GREEN);
        //设置进度条的背景颜色
        srl.setBackgroundColor(Color.YELLOW);
        //设置下拉多少开始刷新
        srl.setDistanceToTriggerSync(70);

        button1 = (AppCompatButton) findViewById(R.id.button1);
        button1.setOnClickListener(this);

        button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(this);

        String items[] = {"条目0", "条目1", "条目2", "条目3", "条目4", "条目5", "条目6"};
        adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, items);
    }

    @Override
    public void onClick(View view){
        switch(view.getId()){
            case R.id.showDialog:
                showDialog();//显示dialog
                break;
            case R.id.button1:
                showPopupMenu(view);//显示popupMenu
                break;
            case R.id.button2:
                showPopup(view);//显示ListPopupWindow
            default:
                break;
        }
    }

    public void showDialog(){
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        //设置标题
        alertDialogBuilder.setTitle("傻逼");
        //设置内容
        alertDialogBuilder.setMessage("傻逼,你好");
        //设置确定按钮的点击事件
        alertDialogBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        //设置取消按钮的点击事件
        alertDialogBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        //显示对话框
        alertDialogBuilder.show();
    }

    public void showPopup(View view){
        final ListPopupWindow  listPopupWindow = new ListPopupWindow(this);
        listPopupWindow.setAdapter(adapter);//设置作为数据源的适配器
        listPopupWindow.setAnchorView(view);//设置锚点
        listPopupWindow.setWidth(400);//设置宽
        listPopupWindow.setHeight(500);//设置高
        listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, "点了第"+position, Toast.LENGTH_SHORT).show();
                //listPopupWindow.dismiss();
            }
        });
        listPopupWindow.show();


    }

    public void showPopupMenu(View view){
        PopupMenu popupMenu = new PopupMenu(this, view);
        popupMenu.getMenuInflater().inflate(R.menu.main, popupMenu.getMenu());//动态加载main.xml
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {//设置item监听
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Toast.makeText(MainActivity.this, item.getTitle()+"is press", Toast.LENGTH_SHORT).show();
                return false;
            }
        });
        popupMenu.show();//显示PopupMenu
    }
}


运行结果

Material Design的一些常用控件_第1张图片

(Android原生的Button和Material Design中的Button并没有什么太多的不同)


Dialog

Material Design的一些常用控件_第2张图片


显示PopupMenu(showPopupMenu方法,该方法中为PopupMenu加载了main.xml布局)

Material Design的一些常用控件_第3张图片


main.xml

Material Design的一些常用控件_第4张图片

Material Design的一些常用控件_第5张图片

你可能感兴趣的:(Material,Design笔记,Material,Design)