2017-01-31 20 views
1

私はボタンをクリックしたときに画像を追加するLayoutParamsを使用して、幅と高さを変更しようとしていました。ヌルリファレンス、問題は静的ではないので、私は参照を行うことができませんImageViewプログラムで追加した画像サイズを変更する方法

これは私がLinearLayoutの内側に画像を追加する方法です:

public void AddNewImages(Context context,Bitmap bitmap){ 
    ImageView img = new ImageView(context); 
    img.setScaleType(ImageView.ScaleType.FIT_XY); 
    img.setImageBitmap(bitmap); 
    linearImages.addView(img); 
    bitmapArray.add(bitmap); 
    indexTags++; 
} 

はXML:

<LinearLayout 
    android:orientation="vertical" 
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <HorizontalScrollView 
     android:orientation="horizontal" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
     <LinearLayout 
      android:orientation="horizontal" 
      android:id="@+id/linearImages" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 
      <Button 
       android:layout_gravity="center" 
       android:id="@+id/add_btn" 
       android:text="Add" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 
     </LinearLayout> 
    </HorizontalScrollView> 
+0

私はこれを置く必要がありますリニアイメージの子供から – Pavya

答えて

2

まずあなたが最初にそれのためにlayoutParametersを設定する必要がありますので、あなたが動的にImageViewのを作成していると、あなたは高さを設定することができます

for(int i=0;i<linearImages.getChildCount();i++) 
{ 
    ImageView image=(ImageView)linearImages.getChildAt(i); 
    image.getLayoutParams().height=100; 
    image.getLayoutParams().width=100; 
} 
+0

イメージビューを取得するには? 'indexTags ++;'の直後ですか?私はしようとしたが、何かエラーを与えることなく私のアプリクラッシュ –

+0

それは、ありがとう! –

0

、その後LayoutParamを設定します。

0

つまり、あなたがのLinearLayoutに追加ImageViewのを見つけ、サイズ を設定する必要がありますし、画像ビューの幅。これを参照してくださいNullPointerException while setting LayoutParams質問

関連する問題