Android programming rewrite ViewGroup way to implement card layout _android

Source: Internet
Author: User

The example of this article describes the Android programming rewrite ViewGroup to implement the card layout method. Share to everyone for your reference, specific as follows:

implementation of the effect as shown in figure:

Realize the idea

1. Rewrite onmeasure (int widthmeasurespec, int heightmeasurespec) to set the size of each child view

2. Rewrite OnLayout (boolean changed, int l, int t, int r, int b) to set the position of each child view

The first step: new FlowLayout inheritance ViewGroup

Package com.rong.activity;
Import Android.content.Context;
Import Android.util.AttributeSet;
Import Android.view.View;
Import Android.view.ViewGroup; /** * Card Layout * * @author Xu Rong * */public class FlowLayout extends ViewGroup {public FlowLayout
  Ibuteset attrs) {Super (context, attrs);  @Override protected void OnLayout (Boolean changed, int l, int t, int r, int b) {//Number of current child view int childsize
    = Getchildcount ();
    Gets the line width int linewidth = Getmeasuredwidth ();
    is currently the first few lines of int lines = 1;
    The current cumulative line width int nowlinewidth = 0;
      for (int i = 0; i < childsize i++) {View view = Getchildat (i);
      The width of the child view int childwidth = View.getmeasuredwidth ();
      The height of the child view int childheight = View.getmeasuredheight (); If the current nowlinewidth+childwidth>= linewidth is a newline if (nowlinewidth + childwidth >= linewidth) {nowlinew
        idth = 0;
      lines = lines + 1; //Set the position of the child View VIew.layout (Nowlinewidth, Childheight * (lines-1), Nowlinewidth + childwidth, childheight * lines);
      Nowlinewidth = Nowlinewidth + childwidth;
        If Nowlinewidth >= linewidth, the newline if (nowlinewidth >= linewidth) {nowlinewidth = 0;
      lines = lines + 1; }} @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (Widt
    Hmeasurespec, Heightmeasurespec);
    Set the size of their view setmeasureddimension (Widthmeasurespec, Heightmeasurespec);
      for (int i = 0; i < Getchildcount (); i++) {View view = Getchildat (i);
    Sets the size of each child view View.measure (View.getmeasuredwidth (), View.getmeasuredheight ());

 }
  }
}

Step Two: Create a new layout file

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:background=" @android: Color/black "android:orientation=" vertical "> <com.rong.activity.flowlayout android:layout_width=" match_pare NT "android:layout_height=" Match_parent "android:background=" #ffffff "> <button android:layout_wid Th= "Wrap_content" android:layout_height= "wrap_content" android:text= "Apple"/> <button Android
      : layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "button"/> <Button Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Cup"/> ; Button android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "Double"/&
    Gt <button Android:laYout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Ear"/> <Button A Ndroid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Flower"/> <Bu
    Tton android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "Game"/> <button android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Ho Tdog "/> <button android:layout_width= wrap_content" android:layout_height= "Wrap_content" Andr oid:text= "interseting"/> <button android:layout_width= "Wrap_content" android:layout_height= Ntent "android:text=" Joker "/> <button android:layout_width=" Wrap_content "Android:layout_heig ht= "Wrap_content" android:text= "King"/> <button android:layout_width= "Wrap_content" android:l Ayout_height= "Wrap_conteNT "android:text=" Mother "/> <button android:layout_width=" wrap_content "Android:layout_height" = "Wrap_content" android:text= "Lost"/> <button android:layout_width= "Wrap_content" Android:lay
      out_height= "Wrap_content" android:text= "noting"/> <button android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "orange"/> <button android:layout_width= "Wrap_co" Ntent "android:layout_height=" wrap_content "android:text=" Poker "/> <button android:layout_wid Th= "Wrap_content" android:layout_height= "wrap_content" android:text= "qustion"/> <button, Andro
      Id:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Ring"/> <Button
    Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "string"/> <button Android: layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "type"/> <Button Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "unit"/> < Button android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "vertion"/ > <button android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:tex t= "West"/> <button android:layout_width= "wrap_content" android:layout_height= "Wrap_content" a
      ndroid:text= "x"/> <button android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "Young"/> <button android:layout_width= "Wrap_content" android:layout_height=

 P_content "android:text=" Zip "/> </com.rong.activity.FlowLayout> </RelativeLayout>

Run!

More interested readers of Android-related content can view the site's topics: Introduction to Android Development and advanced tutorials, the summary of Android basic components usage, the overview of Android View View techniques, the Android Layout layout skills Summary, and A summary of the usage of Android controls

I hope this article will help you with the Android program.

Related Article

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.