2013-08-05 163 views
0

我有一個線性佈局,其中包含10個圖像(例如啤酒杯)5在上排和5在第二排,但我想動態顯示這些圖像,就像我們從服務器獲得3值上排顯示兩個啤酒杯,中間也顯示兩個啤酒杯,第三個杯子顯示在第二排也顯示在中間。 同樣,如果我們從服務器獲得7個值,那麼將在上排顯示4個杯子,在第二排顯示3個杯子。在佈局中動態顯示圖像

+0

使用GridView與android:numColumns =「auto_fit」 –

+0

同樣的事情,我正在實施.... –

+1

那麼你想從我們這裏得到什麼樣的幫助? – Goofy

回答

0

我認爲你正在尋找這樣的東西。

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(); 
     } 

    }; 

而且actvity_main.xml是這樣的:

<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> 

希望這把你在正確的方向。

+0

圖像被垂直添加,即一個在另一個之上不併排@Roberto Lombardini – veronica

+0

嘿,它的工作很棒...我之前做了一些錯誤..非常感謝你... @Roberto Lombardini – veronica

+0

我很高興它有幫助^^ –