2017-03-09 15 views
1

LinearLayoutの魔法使いにImageViewsを動的に追加するのはHorizo​​ntalScrollViewの内部です。親の親の次元に基づいて幅と高さを設定Android Studio

このHorizo​​ntalScrollViewの親LinearLayoutに基づいて、これらのImageViewの幅と高さを設定します。

この

は私のxml(information_imagescrollview_item.xml)です:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/parentlayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingTop="5dp" 
    android:paddingBottom="5dp"> 
    <HorizontalScrollView 
     android:id="@+id/horizontal_scroll" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
     <LinearLayout 
      android:id="@+id/linear" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:orientation="horizontal" > 
     </LinearLayout> 
    </HorizontalScrollView> 
</LinearLayout> 

、これは私のコードです:私はparentlayoutと同じ幅と高さを持つように各ImageViewのを設定するにはどうすればよい

View contentView = inflater.inflate(R.layout.information_imagescrollview_item, container, false); 
LinearLayout scrollViewLayout = (LinearLayout) contentView.findViewById(R.id.linear); 
for(Object intObj : page.content) { 
    ImageView imageView = new ImageView(this.getContext()); 
    Drawable myDrawable = getResources().getDrawable((int) intObj); 
    imageView.setImageDrawable(myDrawable); 
    scrollViewLayout.addView(imageView); 
} 
page.addView(contentView); 

XMLで?

答えて

1

コードで動的ビューのサイズを変更することは可能です。含む(親)への幅と高さLinearLayout:だから私は、あなたが「

for(Object intObj : page.content) { 
    ImageView imageView = new ImageView(this.getContext()); 
    Drawable myDrawable = getResources().getDrawable((int) intObj); 
    imageView.setImageDrawable(myDrawable); 
    //Changing height... 
    imageView.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT; 
    //...and width 
    imageView.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT; 
    scrollViewLayout.addView(imageView); 
} 

これはImageViewを設定します。このようなあなたのLinearLayoutに追加する前に、forループ内のレイアウトパラメータ」ImageViewを変更する必要があります賭けるだろう幅と高さ。

+0

imageView.getLayoutParams()を追加すると、width = ViewGroup.LayoutParams.MATCH_PARENT;画像は依然として画面幅のほぼ2倍です。 – Walter

+0

ギャラリーアプリのようにイメージを次々にスクロールしますか? – Geryson

+0

はい。しかし、いくつかのテキストビューの間に、 – Walter

関連する問題