问题很简单,我正在使用代码动态创建一个imageview。

ImageView btnSend = new ImageView (this);

并将其添加到LinearLayout中,问题是我想保持右对齐

怎么做?

提前致谢

最佳答案

尝试使用LayoutParams:

LinearLayout rootLayout = (LinearLayout)findViewById(R.id.root_layout);

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.RIGHT;

ImageView btnSend = new ImageView (this);
btnSend.setLayoutParams(params);
rootLayout.addView(btnSend);

唯一的问题是,我不记得params.gravity是设置内容重力还是layout_gravity。 Layout_gravity是您要在此实例中更改的内容。

10-08 08:42