如何在Android中的viewgroup中添加relativeLayout

时间:2013-09-05 08:41:58

标签: java android layout relativelayout viewgroup

我正在使用DraggableGridView。 我想要做的是在RelativLayout中添加一个由ImageView和TextView组成的自定义视图。我可以成功控制onLayout方法中的RelativLayout,但不能成功控制Relative Layout

中的元素

以下是我的代码:

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    //compute width of view, in dp
    float w = (r - l) / (dpi / 160f);

    //determine number of columns, at least 2
    colCount = 1;
    int sub = 240;
    w -= 280;
    while (w > 0)
    {
        colCount++;
        w -= sub;
        sub += 40;
    }

    //determine childSize and padding, in px
    childSize = (r - l) / colCount;
    childSize = Math.round(childSize * childRatio);
    padding = ((r - l) - (childSize * colCount)) / (colCount + 1);

    for (int i = 0; i < getChildCount(); i++)
        if (i != dragged)
        {
            Point xy = getCoorFromIndex(i);
            DeletableView view = (DeletableView) getChildAt(i);
            view.layout(xy.x, xy.y, xy.x + childSize, xy.y + childSize);
            view.setText(poem.get(i));
            view.setButtonVisible();
        }
}

MyCustomView

public class DeletableView extends RelativeLayout{
private Activity activity;
Button del_btn;
TextView cat_txt;
private int width;
private int height;
@SuppressLint("NewApi")
public DeletableView(Context context, Drawable background, String catName, int catId) {
    super(context);
    activity = (Activity)context;
    LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.deletable_item, this, true);
    this.setBackground(background);
    del_btn = (Button)findViewById(R.id.del_btn);
    cat_txt = (TextView)findViewById(R.id.cat_txt);
    // TODO Auto-generated constructor stub
}

public void setButtonSize(){
    del_btn.setHeight(width/11);
    del_btn.setWidth(width/11);     
}   
public void setText(String text){
    Log.i("", text);

    RelativeLayout.LayoutParams layoutParams = 
            (RelativeLayout.LayoutParams)cat_txt.getLayoutParams();
    layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, 0);
    cat_txt.setLayoutParams(layoutParams);
    cat_txt.setText(text);

}   


public void setButtonVisible(){
    del_btn.setVisibility(View.VISIBLE);
}   
public void setButtonGone(){
    del_btn.setVisibility(View.GONE);
}
public void setViewAlpha(){
    onSetAlpha(100);
}   
public void resetViewAlpha(){
    onSetAlpha(225);
}
public void btnClicked(){
    this.setVisibility(View.GONE);
}

 }

非常感谢你的关注。

0 个答案:

没有答案