Activity《ViewGroup《若干wiew,获取布局大小

当直接赋值时,能在onCreate里直接获取大小

  android:id="@+id/tztViewGroupLinearLayout" 
  android:layout_width="200dip"
  android:layout_height="200dip"

否则要通过线程获取

 

================AndroidParentSize================

package com.parentsize.app;

 

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

public class AndroidParentSize extends Activity {
 LinearLayout root;
 MyViewGroup mvg;
 CRect rect;
 private Handler
 handler = new Handler() {
  @Override
  public void handleMessage(Message msg) {
    int action = msg.what;
    mvg.set(new CRect(0,0,rect.Width(),rect.Height()));
//    pMyThread.stop();
    pMyThread=null;
  }
 };
 MyThread pMyThread;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        Log.e("id", "onCreate");
       
        setContentView(R.layout.main);
       
        root  = (LinearLayout) findViewById(R.id.tztViewGroupLinearLayout);
        mvg = (MyViewGroup) findViewById(R.id.tztViewGroup);
      
        pMyThread = new MyThread();//启动了我们的线程了
        pMyThread.start();
    }
   
 @Override
 protected void onResume() {
  super.onResume();
 }

 class MyThread extends Thread {
  public void run() {
   try {
    while (true) {
     System.out.println("------MyThread");
     int w = root.getWidth();
     int h = root.getHeight();
     if(mvg.rect == null && w > 0 && h > 0){
      rect = new CRect(0,0,w,h);
      handler.sendMessage(Message.obtain(handler, 0));
      break;
     }
    }
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
 }
}

 ================MyViewGroup================

package com.parentsize.app;


import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

public class MyViewGroup extends ViewGroup{
 View view1;
 View view2;
 
 CRect rect;
 CRect rect1;
 CRect rect2;
 
 Context context;
 public MyViewGroup(Context context1) {
  super(context1);

  this.context = context1;
 }
 
 public MyViewGroup(Context context1, AttributeSet attrs) {
  super(context1, attrs);
  this.context = context1;
 }

 public MyViewGroup(Context context1, AttributeSet attrs, int defStyle) {
  super(context1, attrs, defStyle);
  this.context = context1;
 }
 public void set(CRect r){
  rect = r;
  
  rect1 = new CRect(rect.left,rect.top,rect.left+rect.right/2,rect.bottom);
  rect2 = new CRect(rect1.right,rect1.top,rect.right,rect1.bottom);
  
  view1 = new View1(context,rect1);
  view1.setTag("1");
  System.out.println("rect1:"+rect1.left+";"+rect1.top+";"+rect1.right+";"+rect1.bottom+";");
  addView(view1);
  
  view2 = new View2(context,rect1);
  view2.setTag("2");
  addView(view2);
 }
 
 
 
 @Override
 protected void onLayout(boolean changed, int l, int t, int r, int b) {
  View v;
  int tag;
  int i = 0;
  int nCount = getChildCount();
  while (i < nCount) {
   v = getChildAt(i);
   tag = parseInt(v.getTag().toString());
   switch (tag) {
    case 1:
     v.measure(rect1.Width(), rect1.Height());
     v.layout(rect1.left, rect1.top, rect1.right, rect1.bottom);
     break;
    case 2:
     v.measure(rect2.Width(), rect2.Height());
     v.layout(rect2.left, rect2.top, rect2.right, rect2.bottom);
     break;
   }
   i++;
  }
 }

 public static int parseInt(String nStr) {
  int n;
  try {
   n = Integer.parseInt(nStr);
  } catch (Exception e) {
   n = -1;
  }
  return n;
 }
}

================View1================

package com.parentsize.app;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class View1 extends View{
 CRect rect;
 
 public View1(Context context,CRect rect) {
  super(context);
  this.rect = rect;
 }
 
 public void onDraw(Canvas canvas) {
  Paint paint = new Paint();
  paint.setColor(Color.GRAY);
  canvas.drawRect(rect.left, rect.top, rect.right, rect.bottom, paint);
  paint.setColor(Color.GREEN);
  canvas.drawCircle(rect.left + rect.Width() / 3, rect.top + rect.Height() / 3, rect.Width() / 3, paint);
 }
}

 

================View2================

package com.parentsize.app;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class View2 extends View{
 CRect rect;
 
 public View2(Context context,CRect rect) {
  super(context);
  this.rect = rect;
 }
 
 public void onDraw(Canvas canvas) {
  Paint paint = new Paint();
  paint.setColor(Color.RED);
  canvas.drawRect(rect.left, rect.top, rect.right, rect.bottom, paint);
  paint.setColor(Color.BLACK);
  canvas.drawCircle(rect.left + rect.Width() / 2, rect.top + rect.Height() / 2, rect.Width() / 2, paint);
 }
}

 

================main.xml================

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="30dip"
     >
     <Button android:text="@+id/Button02" android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
 </LinearLayout>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/tztViewGroupLinearLayout"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:background="@color/white"
     >
     <com.parentsize.app.MyViewGroup
   android:id="@+id/tztViewGroup"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent" />
 
 </LinearLayout>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="20dip"
     >
     <Button android:text="@+id/Button01" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
 </LinearLayout>
</LinearLayout>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值