0

DraggableGridViewを使用しています。私がやりたいことは、RelativeLayout に 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);
}

 }

ご清聴ありがとうございました。

4

0 に答える 0