2013-10-29 43 views
1

,我在一個Android應用程序 其中 相對佈局在相對佈局動態顯示多ImageViews的Android

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_marginTop="2dp" 
    android:adjustViewBounds="true" 
    android:background="@drawable/background" /> 
<RelativeLayout 
    android:id="@+id/mymainlayout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
    > 

</RelativeLayout> 

我的問題是我不能以編程方式繪製所有ImageViews在其顯示的相對佈局工作只有一個圖像。 這裏是我的代碼

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

     arrayofImages[i] = new ImageView(this); 
     arrayofImages[i].setImageResource(imageId[imageNumber]); 
     arrayofImages[i].setId(i); 
     if(i!=0) 
     { 
     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
     params.addRule(RelativeLayout.LEFT_OF, arrayofImages[i-1].getId()); 
     //arrayofImages[i].setLayoutParams(params); 
     nn.addView(arrayofImages[i],params); 
     } 
    else 
     { 
      nn.addView(arrayofImages[i]); 
     } 

    } 

這裏nn是我的主要佈局

任何幫助,請???

+0

你應該動態地添加圖片觀點,參見[此處輸入鏈接的描述] [1] [1]:http://stackoverflow.com/questions/16942486/add-imageview-dynamic-to-existing-view –

+0

你想要達到什麼目的? –

+0

我想在佈局中顯示10個圖像。我已經使用線性佈局,但它最多隻顯示7幅圖像。重量總和爲10. –

回答

2

您的代碼除了

工作正常
params.addRule(RelativeLayout.LEFT_OF, arrayofImages[i-1].getId()); 

RelativeLayout.LEFT_OF它保持設置圖像的左側,你無法看到它。

替換上面的代碼與

params.addRule(RelativeLayout.RIGHT_OF, 
         arrayofImages[i - 1].getId()); 

這將顯示在水平方向上的圖像。

更新您的for循環這樣

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

      arrayofImages[i] = new ImageView(this); 
      arrayofImages[i].setImageResource(R.drawable.ic_launcher); 
      arrayofImages[i].setId(i); 
      if (i != 0) { 
       RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
         RelativeLayout.LayoutParams.WRAP_CONTENT, 
         RelativeLayout.LayoutParams.WRAP_CONTENT); 
       params.addRule(RelativeLayout.RIGHT_OF, 
         arrayofImages[i - 1].getId()); 
       // arrayofImages[i].setLayoutParams(params); 
       nn.addView(arrayofImages[i], params); 
      } else { 
       nn.addView(arrayofImages[i]); 
      } 

     } 
+0

謝謝@Brontok它的作品:) –

1

如果使用RelativeLayout剛剛舉行imageview S的要添加dymanically,然後使用LinearLayout。這將解決你的問題。

+0

我已經使用線性佈局耳毛,但問題與linearlayout是它不顯示所有10個圖像它只顯示最多7個圖像。不超過七個。重量總和爲10 –

+0

@NoamanAkram,您必須使用滾動視圖才能顯示超過10張圖像。滾動視圖將有另一個線性將有方向以及 –

0

看看,這可能對你有用:

public class MainActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     ViewPagerAdapter adapter = new ViewPagerAdapter(this, imageArra); 
     ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager); 
     myPager.setAdapter(adapter); 
     myPager.setCurrentItem(0); 
    } 

    private int imageArra[] = { R.drawable.gallery_photo_1, R.drawable.gallery_photo_2, 
    R.drawable.gallery_photo_3, R.drawable.gallery_photo_5, 
    R.drawable.gallery_photo_6, R.drawable.gallery_photo_7, 
    R.drawable.gallery_photo_8, R.drawable.ic_launcher }; 

    } 
} 

和ViewPagerAdapter:

public class ViewPagerAdapter extends PagerAdapter { 

Activity activity; 
int imageArray[]; 

public ViewPagerAdapter(Activity act, int[] imgArra) { 
imageArray = imgArra; 
activity = act; 
} 

public int getCount() { 
    return imageArray.length; 
} 

public Object instantiateItem(View collection, int position) { 
    ImageView view = new ImageView(activity); 
    view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
    LayoutParams.FILL_PARENT)); 
    view.setScaleType(ScaleType.FIT_XY); 
    view.setBackgroundResource(imageArray[position]); 
    ((ViewPager) collection).addView(view, 0); 
return view; 
} 

@Override 
public void destroyItem(View arg0, int arg1, Object arg2) { 
((ViewPager) arg0).removeView((View) arg2); 
} 

@Override 
public boolean isViewFromObject(View arg0, Object arg1) { 
return arg0 == ((View) arg1); 
} 

@Override 
public Parcelable saveState() { 
    return null; 
} 
} 

XML文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<android.support.v4.view.ViewPager 
android:id="@+id/myfivepanelpager" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<ImageView 
    android:id="@+id/image" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/ic_launcher" > 

</ImageView> 
</android.support.v4.view.ViewPager> 

</LinearLayout>