Domanda

I'm trying to create a card game for Android, and I'm stuck on a confusing Issue. I have a Custom View called CardBG that extends from the ViewFlipper class, so that i can flip the card around and show the front and the back. this works fine. But i need to add some other things to the Cards, like a Textfield for example. So i created a Viewgroup in the belief that i can simply add Views to it. Adding this ViewGroup to my Activity results in nothing though. What am I doing wrong? Is this a wrong approach alltogether? I've also tried having Card extend a layout-class, such as RelativeLayout, but it gives me the same result.

Here is the relevant code, adding the cards has to be done dynamically, so no xml shenanigans:

TestActivity.java

public class TestActivity extends Activity {
RelativeLayout menuLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu);

    menuLayout = (RelativeLayout) findViewById(R.id.layout_menu);

    Card c = new Card(this, null);
    c.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    menuLayout.addView(c);
    }
}

Card.java

public class Card extends ViewGroup{
CardBG background;

TextView text1;

public Card(Context context, AttributeSet attrs) {
    super(context, attrs);
    Log.w("Card", "Constructor");

    background = new CardBG(context, null);
    background.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    this.addView(background);
    }

(protected void onLayout is also in this file, but i do nothing in that method except calling super.onLayout)
}

CardBG.java

public class CardBG extends ViewFlipper{

ImageView blue;
ImageView red;

public CardBG(Context context, AttributeSet attrs) {
    super(context, attrs);
    Log.w("CardBG", "Constructor");

    blue = new ImageView(context);
    blue.setImageResource(R.drawable.card_blue);
    blue.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    this.addView(blue);

    red = new ImageView(context);
    red.setImageResource(R.drawable.card_red);
    red.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    this.addView(red);

    //from here on out there are only onclick listener to test the flipping animations

}
È stato utile?

Soluzione

While extending ViewGroup. You must have to implement onLayout method. In onLayout you need to call layout method on each child of this ViewGroup and provide desired position (relatively to parent) for them. You can check source code of FrameLayout (one of the simpliest subclasses of ViewGroup) to find out how it works.

Although, you may extend your view from RelativeLayout, LinearLayout or simple FrameLayout instead. RelativeLayout would give onLayout implementation by itself and provide relative positions to its children.

EDIT: You might need to inflate layout in current view. Sample Code:

 public class Card extends RelativeLayout {

    public Card(Context context, AttributeSet attr) {
          super(context, attr);
          View.inflate(context, R.layout.my_card_layout, this);
     }
    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top