2017-03-18 53 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(); 
     } 
    } 

我的目標是獲得一個圖片的寬度,它有它的正確寬度安裝在屏幕之後然後將每張圖片的高度設置爲圖片的寬度。嘗試了一些代碼,這是我最近的一個,但高度不符合寬度。我在這裏錯過了什麼?

謝謝你在前進, 朱利安

回答

1

onCreate的觀點並沒有大小還和常寬將返回0添加OnGlobalLayoutListener到視圖和進去onGlobalLayoutImageView的高度和寬度。然後,你可以設置一個新的LayoutParamsImageView

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

謝謝您的回答!我沒有得到任何錯誤,但我必須改變一些東西,以使寬度=高度? – user7342339

+0

請檢查編輯。您需要爲每個ImageView設置一個新的'LayoutParams',並將width和height的值設置爲'beispiel'的寬度。 –

+0

是的,我看到了,它的工作,非常感謝你! – user7342339