Use of FloatingActionButton, TextInputLayout, Snackbar, and TabLayout for new Android M controls

Source: Internet
Author: User

Use of FloatingActionButton, TextInputLayout, Snackbar, and TabLayout for new Android M controls

 

Recently, at Google's 2015 I/O conference, the new Android version M was released. It seems that Android has not been named as a digital version since this version.

At this I/O conference, Google has not significantly changed Android, mainly by modifying and improving the previous Android L version. But it was launched at Google. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Authorization + signature/Signature + HJz9KyvMzQ + signature/Signature + KjrMirw + signature + TUTJ6LzGt + fingerprint + LO/fingerprint + m8/qOh1 + signature/fingerprint + signature/2Lz + oaM8L3A + DQo8aDIgaWQ9 "prerequisite"> prerequisite

To use these material design components, you need to update the Extras support library in the latest SDK, such:

Ps: In the country facing up, such updates need to be turned over or used as agents. You can find a solution on your own.

After the update, introduce the following package under the build. gralde file:

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

Now, we can start using the Material Design component! Let's see what's special about the new component!

FloatingActionButton

As the name suggests: This is a floating button. Come on! Ps: No UI blog is very painful.

The above are three floatingactionbuttons with different effects. The XML layout code is as follows:

  

Because FloatingActionButton overrides ImageView, all floatingactionbuttons have all attributes of ImageView. To

To control the FloatingActionButton size, background color, and shadow depth, we can use the following attributes to control these effects:

App: fabSize: FloatingActionButton size. Two values are assigned: "mini" and "normal". The default value is "normal ". app: backgroundTint: background color of FloatingActionButton. The default background color is in Theme.

  
   #ff0000
  

Color. For more information about how to use the Toolbar and Theme of Android5.x, see http://blog.csdn.net/feiduclear_up/article/details/46457433.
3. app: elevation: FloatingActionButton shadow depth. The shadow depth is shadow by default. If you think the shadow depth is large by default, you can change this attribute to modify the shadow depth.

The preceding three XML layout codes are as follows:

  
                              
  

Notes

The background color of FloatingActionButton cannot be changed through the android: background attribute, but only through the app: backgroundTint attribute, because FloatingActionButton is inherited from ImageView. TextInputLayout

This control is used in the EditView input box. It mainly solves the problem that the hint attribute prompt disappears when EditView received focus editing.

It is not very good when there are multiple EditView input boxes, because it is very likely that the user does not know the need for the current EditView after entering multiple editviews

Enter the content. To solve this problem, TextInputLayout was born. TextInputLayout is inherited from LinearLayout

Layout, so we need to include the EditView in TextInputLayout to use it. The implication is that TextInputLayout cannot be a single

Use only. If you don't talk nonsense, go first:

The XML layout code is as follows:

         
      
  

The Code also shows that TextInputLayout contains EditView.

To achieve the above results, we also need to add the following code:

Final TextInputLayout inputLayout = findView (R. id. textInput); inputLayout. setHint (enter the name :); EditText editText = inputLayout. getEditText (); editText. addTextChangedListener (new TextWatcher () {@ Override public void beforeTextChanged (CharSequence s, int start, int count, int after) {}@ Override public void onTextChanged (CharSequence s, int start, int before, int count) {if (s. length ()> 4) {inputLayout. setErrorEnabled (true); inputLayout. setError (Name Length cannot exceed 4);} else {inputLayout. setErrorEnabled (false); }}@ Override public void afterTextChanged (Editable s ){}});

TextInputLayout not only enables the EditView prompt to pop up on the EditView, but also displays the error information under the EditView.

Common Methods for TextInputLayout are as follows:

SetHint (): Set the prompt. GetEditText (): Get the EditView control in TextInputLayout. SetErrorEnabled (): sets whether to Display error messages. SetError (): Set the pop-up error message when the user inputs an error.

Notes

TextInputLayout cannot be used independently. You need to wrap the EditView component.

[Reprint please indicate the source: http://blog.csdn.net/feiduclear_up/article/details/46500865 CSDN ruins tree]

Use of Snackbar

Snackbar provides a lightweight control between Toast and AlertDialog, which can easily provide message prompts and action feedback.

A lot of nonsense, sister ,:

The use of Snackbar is similar to that of Toast. The call code is as follows:

Final Snackbar snackbar = Snackbar. make (inputLayout, test pop-up prompt, Snackbar. LENGTH_LONG); snackbar. show (); snackbar. setAction (cancel, new View. onClickListener () {@ Override public void onClick (View v) {snackbar. dismiss ();}});

The first parameter View can be any view object in the current parent layout. The subsequent parameters are the same as the Toast parameters. Snackbar is supported.

Set Action events by using public Snackbar setAction (CharSequence text, View. OnClickListener listener). You can also set multiple Action events for Snackbar. The font color of Action uses the following colors in the system subject by default.


  
   #ff0000
  

Of course, you can use the code to change the font color of the Action: Snackbar setActionTextColor (int color );

Note:

Snackbar allows you to set multiple Action events at the same time. Snackbar pops up from the bottom of the interface. TabLayout

Tabs Tab. The effect is similar to that of Netease news client. In fact, there are many methods to implement the Tabs tab, and there are also many good methods on Github.

The open-source control that Google uses is only official this time, so that developers can use it conveniently without referencing third-party libraries. :

The XML layout is as follows:

App: tabSelectedTextColor = @ android: color/holo_blue_bright
  App: tabTextColor = @ android: color/black
  App: tabIndicatorColor = @ android: color/holo_blue_bright android: layout_width = match_parent android: layout_height = wrap_content/>

There are three common attributes:

App: tabSelectedTextColor: Color of the Selected Tab font app: tabTextColor: Color of the unselected Tab font app: tabIndicatorColor: The color of the Tab indicator

Common TabLayout methods are as follows:
-AddTab (TabLayout. Tab tab, int position, boolean setSelected) is added to layout.
-AddTab (TabLayout. Tab tab, boolean setSelected) is the same
-AddTab (TabLayout. Tab tab) is the same
-GetTabAt (int index) to obtain the tab
-GetTabCount (): returns the total number of tabs.
-GetTabGravity (): returns the Gravity of the tab.
-GetTabMode (): returns the tab mode.
-GetTabTextColors (): Obtain the text color in the tab.
-NewTab (): Create a new tab
-RemoveAllTabs () removes all tabs.
-RemoveTab (TabLayout. Tab): removes the specified tab.
-RemoveTabAt (int position): removes the tab at the specified position.
-SetOnTabSelectedListener (TabLayout. OnTabSelectedListener onTabSelectedListener) adds a selection listener for each tab.
-SetScrollPosition (int position, float positionOffset, boolean updateSelectedText)
-SetTabGravity (int gravity): Set Gravity.
-SetTabMode (int mode): sets the Mode. There are two values: TabLayout. MODE_SCROLLABLE and TabLayout. MODE_FIXED indicates whether horizontal sliding is supported when the tab content exceeds the screen width. The first option supports sliding, and the second option does not. horizontal sliding is not supported by default.
-SetTabTextColors (ColorStateList textColor): Set the text color in the tab.
-SetTabTextColors (int normalColor, int selectedColor)
-SetTabsFromPagerAdapter (PagerAdapter adapter): Set PagerAdapter
-SetupWithViewPager (ViewPager viewPager) is linked with ViewPager

Generally, TabLayout is used together with ViewPager to take advantage of it. Now we can use the code to see how to use the above method.

ViewPager = findView (R. id. viewPager); tabLayout = findView (R. id. tabs); List
  
   
TabList = new ArrayList <> (); tabList. add (Tab1); tabList. add (Tab2); tabList. add (Tab3); tabLayout. setTabMode (TabLayout. MODE_FIXED); // sets the tab mode. Currently, it is the default tabLayout mode. addTab (tabLayout. newTab (). setText (tabList. get (0); // Add the tab tabLayout. addTab (tabLayout. newTab (). setText (tabList. get (1); tabLayout. addTab (tabLayout. newTab (). setText (tabList. get (2); List
   
    
FragmentList = new ArrayList <> (); for (int I = 0; I <tabList. size (); I ++) {Fragment f1 = new TabFragment (); Bundle bundle = new Bundle (); bundle. putString (content, tree of the http://blog.csdn.net/feiduclear_up CSDN ruins); f1.setArguments (bundle); fragmentList. add (f1);} TabFragmentAdapter fragmentAdapter = new TabFragmentAdapter (getSupportFragmentManager (), fragmentList, tabList); viewPager. setAdapter (fragm EntAdapter); // set the adapter tabLayout. setupWithViewPager (ViewPager) for viewPager; // associate TabLayout with ViewPager. TabLayout. setTabsFromPagerAdapter (fragmentAdapter); // set an adapter for Tabs
   
  

I will not explain it. There are comments. Let's take a look at the TabFragmentAdapter and TabFragment implementations of the above Code as follows:

TabFragmentAdapter

public class TabFragmentAdapter extends FragmentStatePagerAdapter {    private List
  
    mFragments;    private List
   
     mTitles;    public TabFragmentAdapter(FragmentManager fm, List
    
      fragments, List
     
       titles) {        super(fm);        mFragments = fragments;        mTitles = titles;    }    @Override    public Fragment getItem(int position) {        return mFragments.get(position);    }    @Override    public int getCount() {        return mFragments.size();    }    @Override    public CharSequence getPageTitle(int position) {        return mTitles.get(position);    }}
     
    
   
  

TabFragment

public class TabFragment extends Fragment {    private String content;    private View view;    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        view = inflater.inflate(R.layout.item, container,false);        return view;    }    @Override    public void onActivityCreated(@Nullable Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);        content = getArguments().getString(content);        TextView tvContent = (TextView) view.findViewById(R.id.tv_tab_content);        tvContent.setText(content + );    }}

Note:: Why does the tab on the Tabs tab not support horizontal sliding when the content in Tabs exceeds the screen width of the mobile phone? In fact, TabLayout supports horizontal sliding. You only need to add the following line to the Code:

TabLayout. setTabMode (TabLayout. MODE_SCROLLABLE); // you can specify the tab mode.

The length is a little long. For the CoordinatorLayout, CollapsingToolbarLayout, and AppBarLayout, NavigationView will

Learn from the next blog. If you have any questions about the above Code, you are welcome to discuss it together.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.