Android 在布局中动态显示图像

Android 在布局中动态显示图像,android,Android,我有一个线性布局,其中包含10个图像(例如啤酒杯),5个在上面一行,5个在第二行,但我想动态显示这些图像,就像我们从服务器获得3个值一样,然后两个啤酒杯将显示在上面一行的中间,第三个啤酒杯将显示在第二行的中间。 同样,若我们从服务器得到7个值,那个么上面一行将显示4个杯子,第二行将显示3个杯子 我想你在找这样的东西 import java.util.Random; import android.os.Bundle; import android.view.View; import androi

我有一个线性布局,其中包含10个图像(例如啤酒杯),5个在上面一行,5个在第二行,但我想动态显示这些图像,就像我们从服务器获得3个值一样,然后两个啤酒杯将显示在上面一行的中间,第三个啤酒杯将显示在第二行的中间。
同样,若我们从服务器得到7个值,那个么上面一行将显示4个杯子,第二行将显示3个杯子

我想你在找这样的东西

import java.util.Random;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.app.Activity;
import android.content.Context;

public class MainActivity extends Activity {

    private Context context = null;

    private Button btnUpdate = null;

    private LinearLayout firstRow = null;
    private LinearLayout secondRow = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.context = this;

        this.firstRow = (LinearLayout) this
                .findViewById(R.id.ac_main__firstrow);
        this.secondRow = (LinearLayout) this
                .findViewById(R.id.ac_main__secondrow);

        this.btnUpdate = (Button) this.findViewById(R.id.ac_main__updatebtn);
        this.btnUpdate.setOnClickListener(onClickUpdateRow);


    }

    // I used this mothods only because i don't have a server that give me the value
    private int generateRandomInt() {
        Random rdm = new Random();
        return rdm.nextInt(9);
    }

    // Generate a new ImageView
    private ImageView generateImageView() {
        ImageView beerIcon = new ImageView(this.context);
        //Custumize this with your drawable
        beerIcon.setImageDrawable(this.getResources().getDrawable(R.drawable.ic_launcher));
        return beerIcon;
    }

    private void updateImageRow() {
        int rdmValue = this.generateRandomInt();

        int rowOneImageNumber = rdmValue / 2;

        int rowTwoImageNumber = rdmValue - rowOneImageNumber;

        this.firstRow.removeAllViewsInLayout();
        this.secondRow.removeAllViewsInLayout();

        if (rowOneImageNumber >= rowTwoImageNumber) {

            for (int i = 0; i <= rowOneImageNumber; i++) {
                this.firstRow.addView(this.generateImageView());
            }
            for (int i = 0; i <= rowTwoImageNumber; i++) {
                this.secondRow.addView(this.generateImageView());
            }

        } else {

            for (int i = 0; i <= rowTwoImageNumber; i++) {

                this.firstRow.addView(this.generateImageView());
            }
            for (int i = 0; i <= rowOneImageNumber; i++) {
                this.secondRow.addView(this.generateImageView());
            }
        }
    }

    private OnClickListener onClickUpdateRow = new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            updateImageRow();
        }

    };

希望这能帮你找到正确的方向。

在android上使用GridView:numColumns=“auto\u-fit”我正在实施的是同一件事……那么你想从我们这里得到什么样的帮助呢?@ShaniGoriwal你能解释一下你是如何实施这一点的吗。我还有来自服务器的图像,它们以行方式显示在imageview中。图像是垂直添加的,即一个在另一个之上,而不是并排添加@Roberto Lombardinihey它工作得很好……我正在做一些工作早前的错误。。非常感谢你@罗伯托·隆巴迪尼
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/ac_main__firstrow"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:orientation="horizontal" >
    </LinearLayout>

    <LinearLayout
         android:id="@+id/ac_main__secondrow"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:orientation="horizontal">
    </LinearLayout>

    <Button
        android:id="@+id/ac_main__updatebtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Update" />

    <!-- other layout element -->

</LinearLayout>