RelativeLayout向右而不是在顶部

时间:2013-02-11 19:14:10

标签: android relativelayout

如何使图像视图彼此相邻而不是彼此重叠?

    public void deckButtonOnClick(View v) {
    ImageView imageView = new ImageView(getApplicationContext());
    imageView.setImageResource(DECK.takeCard());
    imageView.setAdjustViewBounds(true);
    imageView.setLayoutParams(PARAMS);
    imageView.setTag("0");
    imageView.setOnClickListener(new MyOnClickListener());

    ((RelativeLayout) findViewById(R.id.innerRelativeLayout)).addView(imageView);

}

谢谢!

2 个答案:

答案 0 :(得分:0)

在xml中,使用android:layout_toRightOf = id。

在代码中,将imageView.setLayoutParams()与RelativeLayout.LayoutParams一起使用并使用params.addRule(RelativeLayout.RIGHT_OF,id);

答案 1 :(得分:0)

选项1:使用orientation =“horizo​​ntal”,将卡片的容器重做为LinearLayout而不是RelativeLayout。

选项2:使用RelativeLayout,您需要为每个imageView创建一个ID(可以在每次创建一个计数器时递增一个计数器并将其附加到ID)。然后,在创建PARAMS变量时,可以add a rule对齐上一个ImageView的右侧。

PARAMS = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
PARAMS.addRule(RelativeLayout.RIGHT_OF, R.id.imageView1);
imageView.setLayoutParams(PARAMS);