2017-03-18 13 views
0

그래서 나는이 XML 코드를 가지고 :높이를 상대 너비와 일치시키는 방법은 무엇입니까?

<LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:orientation="horizontal" 
      android:layout_weight="1"> 

      <ImageView 
       android:layout_width="0dp" 
       android:layout_height="200dp" 
       android:layout_weight="1" 
       android:background="@drawable/profilbildfrau" 
       android:id="@+id/bild0" 
       > 

      </ImageView> 

      <ImageView 
       android:layout_width="0dp" 
       android:layout_height="200dp" 
       android:layout_weight="1" 
       android:background="@drawable/profilbildfrau" 
       android:id="@+id/bild1"> 

      </ImageView> 

      <ImageView 
       android:layout_width="0dp" 
       android:layout_height="200dp" 
       android:layout_weight="1" 
       android:background="@drawable/profilbildfrau" 
       android:id="@+id/bild2"> 

      </ImageView> 

     </LinearLayout> (...) 

을 그리고 이것은 내 JavaCode입니다 :

ImageView beispiel; 
    ImageView[] profilbilder = new ImageView[21]; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_hauptmenue); 

     beispiel = (ImageView)findViewById(R.id.bild0); 
     int weite = beispiel.getWidth(); 

     profilbilder[0] = (ImageView)findViewById(R.id.bild0); 
     profilbilder[1] = (ImageView)findViewById(R.id.bild1); 
     profilbilder[2] = (ImageView)findViewById(R.id.bild2); 
     profilbilder[3] = (ImageView)findViewById(R.id.bild3); 
     profilbilder[4] = (ImageView)findViewById(R.id.bild4); 
     profilbilder[5] = (ImageView)findViewById(R.id.bild5); 
     profilbilder[6] = (ImageView)findViewById(R.id.bild6); 
     profilbilder[7] = (ImageView)findViewById(R.id.bild7); 
     profilbilder[8] = (ImageView)findViewById(R.id.bild8); 
     profilbilder[9] = (ImageView)findViewById(R.id.bild9); 
     profilbilder[10] = (ImageView)findViewById(R.id.bild10); 
     profilbilder[11] = (ImageView)findViewById(R.id.bild11); 
     profilbilder[12] = (ImageView)findViewById(R.id.bild12); 
     profilbilder[13] = (ImageView)findViewById(R.id.bild13); 
     profilbilder[14] = (ImageView)findViewById(R.id.bild14); 
     profilbilder[15] = (ImageView)findViewById(R.id.bild15); 
     profilbilder[16] = (ImageView)findViewById(R.id.bild16); 
     profilbilder[17] = (ImageView)findViewById(R.id.bild17); 
     profilbilder[18] = (ImageView)findViewById(R.id.bild18); 
     profilbilder[19] = (ImageView)findViewById(R.id.bild19); 
     profilbilder[20] = (ImageView)findViewById(R.id.bild20); 



     for(int i = 0; i < 20; i++){ 
      profilbilder[i].setMaxHeight(weite); 
      //ImageView bild = profilbilder[i]; 
      //bild.getLayoutParams().height = weite; 
      profilbilder[i].setMinimumHeight(weite); 
      profilbilder[i].requestLayout(); 
     } 
    } 

내 목표는 화면을 맞는 올바른 폭 후 사진 중 하나의 폭을 얻을 것입니다 각 그림의 높이를 그림의 너비로 설정하십시오. 몇 가지 코드를 시도했지만이 코드는 최근 코드이지만 높이가 너비와 일치하지 않습니다. 내가 여기서 무엇을 놓치고 있니?

아직 onCreate보기가 크기되지에서 미리 줄리안

답변

1

감사 정상적으로 폭은 0이보기에 OnGlobalLayoutListener을 추가하고 onGlobalLayout 내부의 ImageView의 높이와 너비를 얻을 반환합니다.

beispiel.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
     @Override 
     public void onGlobalLayout() { 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 
       beispiel.getViewTreeObserver().removeOnGlobalLayoutListener(this); 
      } else { 
       beispiel.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
      } 

      int width = beispiel.getWidth(); 
      for(int i = 0; i <= 20; i++){ 
       // make the height and width of each ImageView equal to width of beispiel 
       profilbilder[i].setLayoutParams(new LinearLayout.LayoutParams(width, width)); 
      } 
     } 
    }); 
+0

대답 주셔서 감사합니다 : 그럼 당신은 새로운 LayoutParamsImageView마다에 설정할 수 있습니다! 나는 어떤 오류도 가지지 않지만 너비 = 높이를 만들기 위해 뭔가를 바꿔야합니까? – user7342339

+0

수정 사항을 확인하십시오. 각 ImageView에 대해 새로운 LayoutParams를 설정하고 너비와 높이 값을 beispiel의 너비로 설정해야합니다. –

+0

예, 알았습니다. 작동했습니다. 대단히 감사합니다! – user7342339