安卓顶部标题
安卓顶部标题
胡欢哥哥 发表于2年前
安卓顶部标题
  • 发表于 2年前
  • 阅读 61
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

摘要: 顶部导航

package com.example.widget;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
public class MyIndTitle extends LinearLayout {

 public MyIndTitle(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  // TODO Auto-generated constructor stub
 }

 public MyIndTitle(Context context) {
  super(context);
  // TODO Auto-generated constructor stub
 }

 private int everyMargin=20;//��߾����ұ�Ϊ20.
 private int topMargin=20;//��ʦΪ20��
 public int allWith;
 private int allWidth;
 private int allHeight;
 public MyIndTitle(Context context, AttributeSet attrs) {
  super(context, attrs);
  // TODO Auto-generated constructor stub


 }
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  // TODO Auto-generated method stub
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  int childCount = getChildCount();
  allWidth = getMeasuredWidth();
  for (int i = 0; i <childCount; i++) {
   View view = getChildAt(i);
   view.measure(MeasureSpec.UNSPECIFIED,MeasureSpec.UNSPECIFIED);
  }
  setMeasuredDimension(allWidth,allHeight);
 }
 @Override
 protected void onLayout(boolean changed, int l, int t, int r, int b) {
  // TODO Auto-generated method stub
  super.onLayout(changed, l, t, r, b);
  Log.e("para",allWidth+"");
  for (int i = 0; i <getChildCount(); i++) {
   View childView = getChildAt(i);
   if(i==0){
    topMargin=20;//������Ϊ20 ��Ϊִ�ж���
    childView.layout(everyMargin,topMargin,everyMargin+childView.getMeasuredWidth(),topMargin+childView.getMeasuredHeight());

   }
   else {
    View prechildView = getChildAt(i-1);
    int x=prechildView.getRight()+everyMargin;
    if(x+childView.getMeasuredWidth()+everyMargin<=allWidth){
     childView.layout(x, topMargin,x+childView.getMeasuredWidth(),topMargin+childView.getMeasuredHeight());
    }
    else {
     childView.layout(everyMargin,prechildView.getBottom()+20,everyMargin+childView.getMeasuredWidth(),prechildView.getBottom()+20+childView.getMeasuredHeight());
     topMargin=prechildView.getBottom()+20;
     allHeight=prechildView.getBottom()+20+childView.getMeasuredHeight();//�����õ�ʵ�ʵĸ�
    }
   }}

 }

 @Override
 protected void dispatchDraw(Canvas canvas) {
  // TODO Auto-generated method stub
  super.dispatchDraw(canvas);
 }

 

 

 

}

标签: 顶部指标切换
共有 人打赏支持
粉丝 2
博文 5
码字总数 1646
×
胡欢哥哥
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: