Material Design 控件

Material Design 新控件

SwipeRefreshLayout

只能包含一个子View,setOnRefreshListener设置下拉刷新监听

问题:以下布局会出现下拉和列表滑动冲突问题



	

		

		

		
		
		
	

解决:

rvShow.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
        @Override
        public void onScrollChanged() {
            if(rvShow.getChildCount() > 0 && rvShow.getChildAt(0).getTop() == 0){
                srShow.setEnabled(true);
            }else{
                srShow.setEnabled(false);
            }
        }
    });

参考:

https://stackoverflow.com/questions/26295481/android-swiperefreshlayout-how-to-implement-canchildscrollup-if-child-is-not-a-l/26296897#26296897 https://developer.android.com/samples/SwipeRefreshMultipleViews/src/com.example.android.swiperefreshmultipleviews/MultiSwipeRefreshLayout.html#l88

TextInputLayout

依赖:compile 'com.android.support:design:22.2.0'

 compile 'com.android.support:appcompat-v7:22.2.0'

使用TextInputLayout创建一个登陆界面:

http://www.jcodecraeer.com/a/basictutorial/2015/0821/3338.html

AutoCompleteTextView

AutoCompleteTextView 简单用法 :

http://blog.csdn.net/i_lovefish/article/details/17337999

DrawerLayout

位置:android-support-v4.jar

布局文件layout_drawer.xml




    
    
        
        
    

    
    
        
    

java代码

DrawerLayout drawer_layout = (DrawerLayout) findViewById(R.id.drawer_layout);
LinearLayout menu_frame = (LinearLayout) findViewById(R.id.menu_frame);
ListView list_drawer = (ListView) findViewById(R.id.list_drawer);
//左侧Drawer设置内容
mPlanetTitles = new String[]{"菜单1","菜单2","菜单3"};
list_drawer.setAdapter(new ArrayAdapter(MainActivity.this,
        android.R.layout.simple_expandable_list_item_1, mPlanetTitles));
list_drawer.setOnItemClickListener(new MyOnItemClickListener());
//主界面内容替换
MainFragment mainFragment = new MainFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, mainFragment, "main");
fragmentTransaction.commit();

//监听左侧Drawer点击事件
toggle = new MyActionBarDrawerToggle(MainActivity.this,
            drawer_layout, toolbar, R.string.drawer_open, R.string.drawer_close);
toggle.syncState();  //实现左上角图标状态切换
drawer_layout.setDrawerListener(toggle);

 private class MyActionBarDrawerToggle extends ActionBarDrawerToggle {
    public MyActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout, Toolbar toolbar,int openDrawerContentDescRes, int closeDrawerContentDescRes) {
        super(activity, drawerLayout, toolbar, openDrawerContentDescRes, closeDrawerContentDescRes);
    }
    @Override
    public void onDrawerClosed(View drawerView) {Log.d(TAG,"closed");}
    @Override
    public void onDrawerOpened(View drawerView) {Log.d(TAG, "Opened");}
    @Override
    public void onDrawerStateChanged(int newState) {Log.d(TAG, "StateChanged");}
}

Toolbar

位置: compile "com.android.support:appcompat-v7:23.2.1"

布局文件

1、主题文件中取消ActionBar 


false
true

2、布局文件中设置





3、res/menu/menu_main.xml中设置菜单




    
    
    

java代码

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Toolbar标题");
setSupportActionBar(toolbar);
//此后获取Toolbar
ActionBar bar = getSupportActionBar();
bar.setDisplayHomeAsUpEnabled(true);  //打开up button

//建立菜单
  @Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    setSearch(menu);
    return true;
}
//对选中的菜单进行操作
 @Override
public boolean onOptionsItemSelected(MenuItem item) {

    if(toggle.onOptionsItemSelected(item)) {
        Log.d(TAG, "logo==========");
        return true;
    }

    switch (item.getItemId()) {
        case android.R.id.home:
            Log.d(TAG, "0000000");
            MainActivity.this.finish();
            break;
        case R.id.action_refresh:
            Toast.makeText(this, "Refresh", Toast.LENGTH_SHORT).show();
            return true;
        case R.id.action_search:
            Toast.makeText(this, "search", Toast.LENGTH_SHORT).show();
            return true;
        default:
    }
    return false;
}

//为ActionBar实现查询功能

private void setSearch(Menu menu) {
    final MenuItem item = menu.findItem(R.id.action_search);
    SearchView sv = (SearchView) MenuItemCompat.getActionView(item);
    if(sv != null){
        sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String arg0) {
                Toast.makeText(MainActivity.this, arg0, Toast.LENGTH_SHORT).show();
                MenuItemCompat.collapseActionView(item);
                return true;
            }
            @Override
            public boolean onQueryTextChange(String arg0) {
                return false;
            }
        });
    }
}

其他

自定义toolbar内容布局

Toolbar toolbar = (Toolbar) findViewById(R.id.sc_sub_toolbar);
if (toolbar != null) {
	toolbar.setTitle("");
}
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
	LayoutInflater mInflater = LayoutInflater.from(actionBar.getThemedContext());
	View mCustomView = mInflater.inflate(R.layout.sc_title_bar, null);
	ActionBar.LayoutParams params = new ActionBar.LayoutParams(
				ActionBar.LayoutParams.MATCH_PARENT,
				ActionBar.LayoutParams.MATCH_PARENT);

	actionBar.setCustomView(mCustomView, params); // 全局
	actionBar.setDisplayShowCustomEnabled(true); // 显示自定义视图
}

默认toolbar左边会有padding,可以设置contentInsetStart去掉



参见:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1118/2006.html

CollapsingToolbarLayout

用于可以折叠的Toolbar,继承至FrameLayout。

设置:

1、CoordinatorLayout作为主布局容器(位置compile 'com.android.support:design:22.2.1')

2、将AppBarLayout布局中的控件用CollapsingToolbarLayout包裹

3、设置CollapsingToolbarLayout布局属性:app:layout_scrollFlags="scroll|exitUntilCollapsed" 它可以控制包含在CollapsingToolbarLayout中的控件(如:ImageView、Toolbar)在响应layout_behavior事件时作出相应的scrollFlags滚动事件(移除屏幕或固定在屏幕顶端)。

4、将要滚动的的view设置属性app:layout_collapseMode="parallax"

5、设置Toolbar的属性,app:layout_collapseMode="pin"和android:layout_height="?attr/actionBarSize"

6、在触发(滚动)事件的view之上设置app:layout_behavior="@string/appbar_scrolling_view_behavior"

参见:

  • Android CollapsingToolbarLayout使用介绍

    https://blog.csdn.net/u013651026/article/details/78959570

  • CoordinatorLayout、AppBarLayout、CollapsingToolbarLayout之间的关系详解

    https://blog.csdn.net/xiatiandefeiyu/article/details/78226544

  • CollapsingToolbarLayout效果实现原理讲解

    https://blog.csdn.net/xiatiandefeiyu/article/details/78476901

  • 关于CoordinatorLayout与Behavior的一点分析: http://www.jianshu.com/p/a506ee4afecb#

Behavior

  • Android CoordinatorLayout之自定义Behavior

    https://www.jianshu.com/p/b987fad8fcb4

  • Android CoordinatorLayout与Behavior使用指南

    https://www.jianshu.com/p/488283f74e69

  • CoordinatorLayout 自定义Behavior并不难

    https://blog.csdn.net/xy51711/article/details/53269576

TabLayout

位置:compile "com.android.support:design:23.2.1"

布局文件



java代码

TabLayout tabLayout = (TabLayout) this.findViewById(R.id.tabLayout);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1").setIcon(R.drawable.ic_home1));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2").setIcon(R.drawable.ic_home2));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3").setIcon(R.drawable.ic_home3));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
// 改变选中tab图片颜色
final int tabIconColorSelect = ContextCompat.getColor(MainActivity.this, R.color.green);
final int tabIconColor = ContextCompat.getColor(MainActivity.this, R.color.black);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            tab.getIcon().setColorFilter(tabIconColorSelect, PorterDuff.Mode.SRC_IN);

        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
            tab.getIcon().setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN);
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
        }
    });

AppBarLayout

它的作用是把AppBarLayout包裹的内容作为一个整体,组成AppBar

FloatingActionButton

 

public class ScrollAwareFABBehavior extends FloatingActionButton.Behavior {

	private static final Interpolator INTERPOLATOR = new FastOutSlowInInterpolator();
	private boolean mIsAnimatingOut = false;

	public ScrollAwareFABBehavior(Context context, AttributeSet attrs) {
		super();
	}

	@Override
	public boolean onStartNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child,
									   final View directTargetChild, final View target, final int nestedScrollAxes) {
		// Ensure we react to vertical scrolling
		return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL
				|| super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
	}

	@Override
	public void onNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child,
							   final View target, final int dxConsumed, final int dyConsumed,
							   final int dxUnconsumed, final int dyUnconsumed) {
		super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
		if (dyConsumed > 0 && !this.mIsAnimatingOut && child.getVisibility() == View.VISIBLE) {
			// User scrolled down and the FAB is currently visible -> hide the FAB
//			animateOut(child);
			child.hide();
		} else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
			// User scrolled up and the FAB is currently not visible -> show the FAB
//			animateIn(child);
			child.show();
		}
	}
}

参见:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0718/3197.html

RecyclerView

http://blog.csdn.net/lmj623565791/article/details/45059587

CardView

Palette

Snackbar

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar snackbar = Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Snackbar.make(v, "ActionClick", Snackbar.LENGTH_LONG)
                                    .setActionTextColor(MainActivity.this.getResources().getColor(R.color.colorTest)).show();
                        }
                    }).setActionTextColor(ContextCompat.getColor(MainActivity.this, R.color.colorTest));
            //设置文本颜色(snackbar_text是文本id,snackbar_action是Action的id,都是TextView控件)
            View view1 = snackbar.getView();
            ((TextView)view1.findViewById(R.id.snackbar_text)).setTextColor(Color.parseColor("#FF00FF"));
            snackbar.show();
        }
    });

你可能感兴趣的:(Material Design 控件)