对齐的ImageView中的LinearLayout由codeImageView、LinearLayout、code

2023-09-11 23:35:23 作者:雨食Infante◎

这个问题很简单,我是动态创建的ImageView使用code。

the question is simple, i am creating a imageview dynamically using code.

ImageView btnSend = new ImageView (this);

和它添加到的LinearLayout,问题是,我要离开右对齐

and add it to a LinearLayout, the problem is that I want to leave right-aligned

该怎么做?

在此先感谢

推荐答案

请尝试使用的LayoutParams:

Try using 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就是你想在这种情况下更改。

The only problem is that I don't remember if params.gravity sets the content gravity or layout_gravity. Layout_gravity is what you're wanting to change in this instance.