Material Design 控件

Material Design 新控件

SwipeRefreshLayout

只能包含一個子View,setOnRefreshListener設置下拉刷新監聽html

問題:如下佈局會出現下拉和列表滑動衝突問題java

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

	<RelativeLayout
		android:layout_width="match_parent"
		android:layout_height="match_parent">

		<android.support.v7.widget.RecyclerView
			android:id="@+id/rv_show"
			android:background="@color/white"
			android:layout_width="match_parent"
			android:layout_height="match_parent">

		</android.support.v7.widget.RecyclerView>

		<RelativeLayout
			android:layout_width="match_parent"
			android:layout_height="match_parent"
			android:background="@color/white"
			android:orientation="vertical">
		<ImageView
			android:id="@+id/iv_empty"
			android:layout_width="120dp"
			android:layout_height="175dp"
			android:layout_gravity="center_horizontal"
			android:contentDescription="@string/app_name"
			android:scaleType="centerCrop"
			android:layout_centerInParent="true"
			android:src="@drawable/img_empty" />
		</RelativeLayout>
	</RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>

解決:react

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

參考:android

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#l88app

TextInputLayout

依賴:compile 'com.android.support:design:22.2.0'ide

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

使用TextInputLayout建立一個登錄界面:佈局

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

AutoCompleteTextView

AutoCompleteTextView 簡單用法 :spa

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

DrawerLayout

位置:android-support-v4.jar

佈局文件layout_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/drawer_layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
    >

    <!--主佈局-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/content_frame">
        </FrameLayout>
    </RelativeLayout>

    <!--側滑Drawer-->
    <LinearLayout
        android:id="@+id/menu_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:background="[@android](http://my.oschina.net/asia):color/white"
        >
        <ListView
            android:id="@+id/list_drawer"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"/>
    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

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<String>(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"

佈局文件

一、主題文件中取消ActionBar 

<!-- 使用 API Level 22 編譯的話,要拿掉前綴android -->
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>

二、佈局文件中設置

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:minHeight="?attr/actionBarSize"
    android:elevation="10dp">
</android.support.v7.widget.Toolbar>

三、res/menu/menu_main.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/action_search"
        android:title="搜尋"
        android:icon="@android:drawable/ic_menu_search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/action_refresh"
        android:title="更新"
        android:icon="@android:drawable/ic_menu_rotate"
        app:showAsAction="withText|ifRoom" />
    <item
        android:id="@+id/action_settings"
        android:title="設置"
        app:showAsAction="never" />
</menu>

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去掉

<android.support.v7.widget.Toolbar
	android:id="@+id/sc_sub_toolbar"
	android:layout_width="match_parent"
	android:layout_height="@dimen/main_toolbar_height"
	app:contentInsetStart="0dp"
	>
</android.support.v7.widget.Toolbar>

參見:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1118/2006.html

CollapsingToolbarLayout

用於能夠摺疊的Toolbar,繼承至FrameLayout。

設置:

一、CoordinatorLayout做爲主佈局容器(位置compile 'com.android.support:design:22.2.1')

二、將AppBarLayout佈局中的控件用CollapsingToolbarLayout包裹

三、設置CollapsingToolbarLayout佈局屬性:app:layout_scrollFlags="scroll|exitUntilCollapsed" 它能夠控制包含在CollapsingToolbarLayout中的控件(如:ImageView、Toolbar)在響應layout_behavior事件時做出相應的scrollFlags滾動事件(移除屏幕或固定在屏幕頂端)。

四、將要滾動的的view設置屬性app:layout_collapseMode="parallax"

五、設置Toolbar的屬性,app:layout_collapseMode="pin"和android:layout_height="?attr/actionBarSize"

六、在觸發(滾動)事件的view之上設置app:layout_behavior="@string/appbar_scrolling_view_behavior"

參見:

Behavior

TabLayout

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

佈局文件

<android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        app:tabIndicatorColor="@color/colorAccent"
        app:tabSelectedTextColor="@android:color/darker_gray"
        app:tabTextColor="@android:color/white"
        >
</android.support.design.widget.TabLayout>

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

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher"
    app:layout_anchor="@id/drawer_layout"
    app:layout_anchorGravity="bottom|end"
    android:layout_margin="16dp"
    app:backgroundTint="#FF87FFEB"
    app:rippleColor="#33728DFF"
    app:elevation="5dp"
    app:pressedTranslationZ="12dp"
    app:layout_behavior="com.egrid.materialdemo.ScrollAwareFABBehavior"
    />

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();
        }
    });
相關文章
相關標籤/搜索