2017-03-09 1 views
1

LinearLayout 마녀에 ImageViews를 동적으로 추가하는 것은 HorizontalScrollView 내부에 있습니다.부모의 부모의 차원을 기반으로 너비와 높이를 설정하십시오. Android Studio

이제이 HorizontalScrollView의 부모 LinearLayout을 기반으로 ImageViews의 너비와 높이를 설정하고 싶습니다.

내 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와 같은 폭과 높이가 각각의 이미지 뷰를 설정하는 방법

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; 이미지는 여전히 화면 폭의 거의 두 배입니다. – Walter

+0

갤러리 응용 프로그램에서와 같이 이미지를 차례로 스크롤 하시겠습니까? – Geryson

+0

예. 하지만 두 텍스트 뷰 사이에 – Walter

관련 문제