Android 프로 그래 밍 다시 쓰기 ViewGroup 카드 레이아웃 구현 방법

7936 단어 AndroidViewGroup
이 사례 는 안 드 로 이 드 프로 그래 밍 이 ViewGroup 을 다시 써 서 카드 레이아웃 을 실현 하 는 방법 을 설명 한다.여러분 께 참고 하도록 공유 하 겠 습 니 다.구체 적 으로 는 다음 과 같 습 니 다.
구현 효 과 는 그림:

사고의 방향 을 실현 하 다.
1.재 작성 onMeasure(int widthMeasureSpec,int height MeasureSpec)각 하위 View 의 크기 설정
2.onLayout 재 작성(boolean changed,int l,int t,int r,int b)각 하위 View 의 위 치 를 설정 합 니 다.
첫 번 째 단계:새로운 FlowLayout 계승 ViewGroup

package com.rong.activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
/**
 *     
 * 
 * @author   
 *
 */
public class FlowLayout extends ViewGroup {
  public FlowLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
  }
  @Override
  protected void onLayout(boolean changed, int l, int t, int r, int b) {
    //    View   
    int childSize = getChildCount();
    //     
    int lineWidth = getMeasuredWidth();
    //       
    int lines = 1;
    //        
    int nowLineWidth = 0;
    for (int i = 0; i < childSize; i++) {
      View view = getChildAt(i);
      //  View   
      int childWidth = view.getMeasuredWidth();
      //  View   
      int childHeight = view.getMeasuredHeight();
      //      nowLineWidth+childWidth>= lineWidth    
      if (nowLineWidth + childWidth >= lineWidth) {
        nowLineWidth = 0;
        lines = lines + 1;
      }
      //    View   
      view.layout(nowLineWidth, childHeight * (lines - 1), nowLineWidth + childWidth, childHeight * lines);
      nowLineWidth = nowLineWidth + childWidth;
      //   nowLineWidth >= lineWidth    
      if (nowLineWidth >= lineWidth) {
        nowLineWidth = 0;
        lines = lines + 1;
      }
    }
  }
  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    //     View   
    setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
    for (int i = 0; i < getChildCount(); i++) {
      View view = getChildAt(i);
      //      View   
      view.measure(view.getMeasuredWidth(), view.getMeasuredHeight());
    }
  }
}

두 번 째 단계:새 레이아웃 파일

<?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_parent"
    android:layout_height="match_parent"
    android:background="#ffffff" >
    <Button
      android:layout_width="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" />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Ear" />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Flower" />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Game" />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Hotdog" />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="interseting" />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="joker" />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="king" />
    <Button
      android:layout_width="wrap_content"
      android:layout_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:layout_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_content"
      android:layout_height="wrap_content"
      android:text="poker" />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="qustion" />
    <Button
      android: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:text="west" />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android: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="wrap_content"
      android:text="zip" />
  </com.rong.activity.FlowLayout>
</RelativeLayout>

운행!
더 많은 안 드 로 이 드 관련 내용 에 관심 이 있 는 독자 들 은 본 사이트 의 주 제 를 볼 수 있다.
본 고 에서 말 한 것 이 여러분 의 안 드 로 이 드 프로 그래 밍 에 도움 이 되 기 를 바 랍 니 다.

좋은 웹페이지 즐겨찾기