Android 如何以编程方式将ImageView放置到相对视图中
我在RelativeLayout中放置了几个图像视图(鸡蛋) 我想让它们在同一行中对齐,但是如果没有足够的位置容纳它们,则只缩小其中的一个,而忽略所有剩余的 有没有办法告诉RelativeLayout如何平均调整它们的大小,使它们都适合屏幕并具有统一的大小(取决于屏幕大小和分辨率)? 但是,如果将所有这些都包括在内,它应该是这样的: () 这是我代码的一部分Android 如何以编程方式将ImageView放置到相对视图中,android,android-layout,imageview,android-relativelayout,Android,Android Layout,Imageview,Android Relativelayout,我在RelativeLayout中放置了几个图像视图(鸡蛋) 我想让它们在同一行中对齐,但是如果没有足够的位置容纳它们,则只缩小其中的一个,而忽略所有剩余的 有没有办法告诉RelativeLayout如何平均调整它们的大小,使它们都适合屏幕并具有统一的大小(取决于屏幕大小和分辨率)? 但是,如果将所有这些都包括在内,它应该是这样的: () 这是我代码的一部分 private void testLoadEggs() { RelativeLayout.LayoutParams params
private void testLoadEggs() {
RelativeLayout.LayoutParams params;
RelativeLayout rl = new RelativeLayout(getContext());
List<ImageView> imageEggs = new ArrayList();
for (int i = 0; i < 11; i++) {
imageEggs.add(new ImageView(getContext()));
SVG svg = SVGParser.getSVGFromResource(this.getResources(), R.raw.white_egg);
imageEggs.get(i).setImageDrawable(svg.createPictureDrawable());
params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(ALIGN_PARENT_BOTTOM);
if (i==0)
{
params.addRule(ALIGN_PARENT_LEFT);
}
else {
params.addRule(RIGHT_OF,i-1);
}
imageEggs.get(i).setId(i);
imageEggs.get(i).setVisibility(View.VISIBLE);
rl.addView(imageEggs.get(i), params);
}
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
params2.addRule(ALIGN_PARENT_BOTTOM);
params2.addRule(CENTER_HORIZONTAL);
this.addView(rl, params2);
}`
与将视图添加到RelativeLayout不同,您可以使用orientation=“horizontal”将视图添加到LinearLayout,并将每个布局设置为android:layout\u weight=“1”您应该使用线性布局,并且您可以轻松地将按钮或图像放置在相同大小的位置。比如,
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_weight="1"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_weight="1"/>
或者你可以多用一些,放你想要的任何东西
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="100"
>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_weight="30"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_weight="30"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_weight="30"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="100"
>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_weight="30"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_weight="30"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_weight="30"/>