我试图动态地对齐几个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>
在我名为Imagem的LinearLayout中的代码中,我添加了更多布局以便给出我的最终结果,这里是添加单行的代码:
LinearLayout layout = (LinearLayout)findViewById(R.id.Imagem);
LinearLayout llay = new LinearLayout(this);
LinearLayout comandos = new LinearLayout(this);
comandos.setOrientation(LinearLayout.HORIZONTAL);
comandos.setGravity(Gravity.RIGHT);
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);
layout.addView(tv);
layout.addView(llay);
llay.addView(tv2);
ImageView image = new ImageView(this);
image.setImageDrawable(e);
comandos.addView(image, new LinearLayout.LayoutParams(Math.round(80*reducao),Math.round(60*reducao)));
llay.addView(comandos, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT,Gravity.RIGHT));
我添加几张图片的方法就是不断创建新的ImageView并将它们添加到&#34; comandos&#34;布局。
是否有任何错误导致正确对齐到最后一行?
答案 0 :(得分:0)
水平LinearLayout不支持Gravity.RIGHT或LEFT,因为它不知道如何处理视图之间留下的“空”空间。
一个非常简单的解决方案是将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添加更多图像,固定答案。