Java 在android中右对齐仅适用于添加的最后一个视图

Java 在android中右对齐仅适用于添加的最后一个视图,java,android,android-layout,Java,Android,Android Layout,我尝试右动态对齐多个ImageView,但只有最后一个是真正对齐的 为了更好地理解,这里有一张图片: 活动布局的设置方式如下: <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true" android:

我尝试右动态对齐多个ImageView,但只有最后一个是真正对齐的

为了更好地理解,这里有一张图片:

活动布局的设置方式如下:

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:id="@+id/scrollView"
        android:layout_below="@+id/button">

        <LinearLayout
            android:id="@+id/Imagem"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

        </LinearLayout>
    </ScrollView>
我添加几个图像的方法就是不断创建新的ImageView并将它们添加到comandos布局中


是否存在仅与最后一行正确对齐的错误?

水平线布局不支持重力。右或左,因为它不知道如何处理视图之间的空白

一个非常简单的解决方案是将TextView的权重设置为1,这样它就可以将图像向右推,并占用空的空间。像这样:

    LinearLayout layout = (LinearLayout)findViewById(R.id.Imagem);
    LinearLayout llay = new LinearLayout(this);
    LinearLayout comandos = new LinearLayout(this);
    comandos.setOrientation(LinearLayout.HORIZONTAL);
    llay.setOrientation(LinearLayout.HORIZONTAL);
    TextView tv = new TextView(this);
    tv.setText(titulo);
    tv.setTypeface(null, Typeface.BOLD);
    tv.setTextSize(tv.getTextSize()*1.05f);
    TextView tv2 = new TextView(this);
    tv2.setText(descricao);
    tv2.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1)); // setting weight to 1
    layout.addView(tv);
    layout.addView(llay);
    llay.addView(tv2);
    ImageView image = new ImageView(this);
    image.setImageDrawable(e);
    image.setLayoutParams(new LinearLayout.LayoutParams(Math.round(80*reducao),Math.round(60*reducao)));
    comandos.addView(image);
    llay.addView(comandos, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));

编辑:我没有意识到你在向comandos添加更多图像,修复了答案。

谢谢你的回答,我用你的更正更新了我的代码,它仍然与我在帖子中显示的图像相同,我不明白为什么权重必须在文本视图中,它不应该在comandos中吗?编辑:实际上刚刚切换到旧版本,而当前版本的权重更偏左
    LinearLayout layout = (LinearLayout)findViewById(R.id.Imagem);
    LinearLayout llay = new LinearLayout(this);
    LinearLayout comandos = new LinearLayout(this);
    comandos.setOrientation(LinearLayout.HORIZONTAL);
    llay.setOrientation(LinearLayout.HORIZONTAL);
    TextView tv = new TextView(this);
    tv.setText(titulo);
    tv.setTypeface(null, Typeface.BOLD);
    tv.setTextSize(tv.getTextSize()*1.05f);
    TextView tv2 = new TextView(this);
    tv2.setText(descricao);
    tv2.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1)); // setting weight to 1
    layout.addView(tv);
    layout.addView(llay);
    llay.addView(tv2);
    ImageView image = new ImageView(this);
    image.setImageDrawable(e);
    image.setLayoutParams(new LinearLayout.LayoutParams(Math.round(80*reducao),Math.round(60*reducao)));
    comandos.addView(image);
    llay.addView(comandos, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));