为什么这不起作用?ALIGN_PARENT_TOP以编程方式不起作用
for (PlayingCard playingCard : Stack0.Cards)
{
ImageView myImg = new ImageView(this);
myImg.setImageResource(R.drawable.c2);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(new ViewGroup.LayoutParams(CardWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
lp.setMargins(0, 0, 0,0);
//lp.addRule(RelativeLayout.ALIGN_TOP); fails
//lp.addRule(RelativeLayout.ALIGN_PARENT_TOP); fails
//lp.addRule(RelativeLayout.ALIGN_START); fails
lp.addRule(RelativeLayout.ALIGN_PARENT_START);
myImg.setLayoutParams(lp);
mat.addView(myImg);
}
成功正在添加的的ImageView的XML
<RelativeLayout
android:id="@+id/PLAY_Mat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
>
</RelativeLayout>
,但它为中心垂直。我希望它对齐到顶部。 我希望这是即使没有添加规则的方式,因为“默认情况下,所有子视图都在布局的左上角绘制”(RelativeLayout文档)。
我将您的解决方案移至社区wiki答案。 –