自定义ViewGroup中未显示复合子视图

时间:2016-10-27 09:47:54

标签: android view viewgroup

我正在编写一个环形菜单,它将把几种不同类型的视图组合在一起,这些视图将具有相同的大小。问题是它与本地视图(例如ImageView)完美配合,但是当我尝试放置自定义标记图像视图时,它根本不会出现在自定义ViewGroup中。还值得一提的是,当这个视图在XML文件中声明时,在自定义ViewGroup之外它显示得很好,但是只要我把它放在ViewGroup里面,或者以编程方式声明它,它就是消失。我的猜测是我在onLayout方法中做了一些事情,但是我不能把手指放在它上面,因为根据日志,所有坐标和视图大小都是正确的。

CompoundView的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true">

<ImageView
    android:id="@+id/header"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:src="@drawable/ropeiconselector"/>

<TextView
    android:id="@+id/label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="WWWWWWWWWWWW"
    android:background="@color/button"
    android:layout_marginLeft="-10dp"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@id/header"
    android:padding="8dp"
    />

CompoundView的代码(我省略了一些不重要的方法)

public class CircularLabeledImageView extends RelativeLayout implements View.OnClickListener {


ImageView headerView;
TextView labelView;

boolean isOpen = false;

String[] itemDesc;

int position;
int size;

int maxLabelWidth = 100;
int minLabelWidth = 20;

final Handler timeHandler = new Handler();
Runnable toggleTimer;

public CircularLabeledImageView(Context context) {
    super(context);

//编辑Methhod打来电话         initView(上下文);

}

public CircularLabeledImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initView(context);
}

public CircularLabeledImageView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initView(context);
}

private void initView(Context context){
    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.expandabletextimageview, this);

    headerView = (ImageView) this.findViewById(R.id.header);
    labelView = (TextView) this.findViewById(R.id.label);

    labelView.setBackgroundResource(R.color.backgroundMenuContents);

    headerView.setOnClickListener(this);

    itemDesc = new String[]{"Item A","Item B", "Item C","Quantos itens"};
    size = itemDesc.length;

    toggleTimer = new Runnable() {
        @Override
        public void run() {
            toggle();
        }
    };

    this.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (isOpen) {
                toggle();
            }
        }
    });

}

}

自定义ViewGroup的代码

public class RingListMenu extends ViewGroup implements View.OnClickListener {

boolean isOpen = true;
int headerSize= 90;
int childSize= 80;
int radiusInit = 150;
int childInitSize = 80;
int radius = 150;
int padding;

DPoint center = new DPoint();

float startingAngle= 2;

DPoint click0;
DPoint clickIni;
DPoint clickFinal;
final static float SENSIBILITY = 10f;
final static float FRICTION = 0.00001f;
final static float MAXVELOCITY = 0.06f;
final static long TOGGLE_DURATION = 300;
private VelocityTracker vTracker = null;
boolean isScrolling;

ImageView circleView;
OnItemClickListener listener = null;

public RingListMenu(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    if (android.os.Build.VERSION.SDK_INT >= 11)
    {
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }
    TypedArray at = context.obtainStyledAttributes(attrs,R.styleable.RingListMenu);
    childInitSize = childSize = at.getDimensionPixelSize(R.styleable.RingListMenu_childSize, 0);
    radiusInit = radius = at.getDimensionPixelSize(R.styleable.RingListMenu_circleRadius, 0);
    headerSize = at.getDimensionPixelSize(R.styleable.RingListMenu_headerSize, 0);
    padding = at.getDimensionPixelSize(R.styleable.RingListMenu_padding, 0);
}

public RingListMenu(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    if (android.os.Build.VERSION.SDK_INT >= 11)
    {
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }
    TypedArray at = context.obtainStyledAttributes(attrs,R.styleable.RingListMenu);
    childInitSize = childSize = at.getDimensionPixelSize(R.styleable.RingListMenu_childSize, 0);
    radiusInit = radius = at.getDimensionPixelSize(R.styleable.RingListMenu_circleRadius, 0);
    headerSize = at.getDimensionPixelSize(R.styleable.RingListMenu_headerSize, 0);
    padding = at.getDimensionPixelSize(R.styleable.RingListMenu_padding, 0);
}

public RingListMenu(Context context, AttributeSet attrs) {
    super(context, attrs);
    if (android.os.Build.VERSION.SDK_INT >= 11)
    {
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }
    TypedArray at = context.obtainStyledAttributes(attrs, R.styleable.RingListMenu);
    childInitSize = childSize = at.getDimensionPixelSize(R.styleable.RingListMenu_childSize, 0);
    radiusInit = radius = at.getDimensionPixelSize(R.styleable.RingListMenu_circleRadius, 0);
    headerSize = at.getDimensionPixelSize(R.styleable.RingListMenu_headerSize, 0);
    padding = at.getDimensionPixelSize(R.styleable.RingListMenu_padding, 0);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    Log.d("RingList", "Calling Ring list onLayout" + childSize + " " + radius + " " + headerSize);

    float angle = (float) (2*Math.PI)/(getChildCount()-1);
    center.x = padding+headerSize/2;
    center.y = padding+headerSize/2;
    float childX;
    float childY;

    //radius = (float) (getChildCount()*(minSpacing+2*childSize)/(2*Math.PI));

    for (int i = 1; i < getChildCount(); i++) {
        View child = getChildAt(i);

        childX = (float) (center.x + radius * Math.cos(startingAngle + i * angle));
        childY = (float) (center.y + radius * Math.sin(startingAngle + i * angle));

        child.layout((int) (childX - childSize / 2), (int) (childY - childSize / 2),
                (int) (childX + childSize / 2), (int) (childY + childSize / 2));
    }

    View header = getChildAt(0);
    header.setX(padding);
    header.setY(padding);
    header.layout(padding, padding, padding + headerSize, padding + headerSize);
}

@Override
public void addView(View child) {
    child.setTag(getChildCount());
    super.addView(child);
    child.setOnClickListener(this);
}

}

最后,声明部分:

    RingListMenu ring = (RingListMenu) findViewById(R.id.ring);
    CircularLabeledImageView ViewA = new CircularLabeledImageView(this);
    ring.addView(ViewA);

0 个答案:

没有答案
相关问题