2013-07-23 25 views
2

我在設計應用程序的佈局時遇到了問題。這就是我試圖讓:Android:使ImageView匹配另一個視圖的高度

========================================================= 
| ============= ================ ================ | 
| |   | | LinearLayout | | LinearLayout | | 
| |   | | ============ | | ============ | | 
| |   | | | TextView | | | | TextView | | | 
| | ImageView | | ============ | | ============ | | 
| |   | | ============ | | ============ | | 
| |   | | | TextView | | | | TextView | | | 
| |   | | ============ | | ============ | | 
| |   | | ============ | | ============ | | 
| |   | | | TextView | | | | TextView | | | 
| |   | | ============ | | ============ | | 
| ============= ================ ================ | 
========================================================= 

這是whay我這樣做的遠(我已經刪除了的ID爲清楚起見其他東西):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:gravity="center_vertical" > 


    <ImageView 
     android:contentDescription="@string/logo" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" /> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:gravity="left|center_vertical" > 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:singleLine="true" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="right|center_vertical" 
     android:orientation="vertical" > 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:singleLine="true" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:singleLine="true" /> 

     <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:singleLine="true" /> 

    </LinearLayout> 

</LinearLayout> 

我問題是我想使用高分辨率圖像並縮小它們以匹配其旁邊的最高線性佈局。我的想法是讓android在低DPI設備上縮小圖像,因此所有平臺都使用相同的圖像和佈局。

現在,主容器高度變得像圖像高度一樣大。我想要的是主容器高度與「最高」線性佈局相同,並強制imageview縮小以適應主容器。

任何想法?

謝謝!

PD:請原諒我的英語! :$

+0

可能的重複[MATCH \ _PARENT如果兄弟視圖較大,WRAP \ _CONTENT如果兄弟視圖較小](http://stackoverflow.com/questions/17119173/match-parent-if-sibling-view-is-更大的包裝內容如果兄弟型視圖更小) – Cris

回答

1

的高度和寬度的所有視圖中的所有的那些東西在onMeasure活性的測量()..的通知值得點是,任何視圖的onMeasure()在onDraw()之前執行並且可以執行n次。因此,可以在onMeasure()中計算LinearLayout的寬度和高度。

+1

儘管你的答案是最不完整的,但它是最有幫助的。其他答案包含對我沒有要求的寬度的限制。根據你的回答,我想我可以創建一個自定義佈局,根據另一個高度來修正它的高度。所以,雖然不是最好的答案,但我仍然選擇你的答案作爲接受的答案。謝謝Abhishek Shukla! –

0
LinearLayout ll = (LinearLayout)findViewById(R.id.your_linearlayout); 

    int llHeight = ll.getHeight(); 

    ImageView iv = (ImageView)findViewById(R.id.your_imageview); 

    LayoutParams ivParams = ((ImageView)findViewById(R.id.your_imageview)).getLayoutParams(); 

    ivParams.height = llHeight; 

    iv.setLayoutParams(ivParams); 
+0

對不起。但是這不會給我三個寬度相同的視圖? LinearLayout的方向是水平的,而不是垂直的。我希望imageview具有與其旁邊的線性佈局相同的高度。但我不想修改寬度。 –

+0

@Gonzalo Brusco爲你必須動態獲得線性佈局高度,然後設置圖像視圖高度。 – TheFlash

+0

我雖然那樣。但是如果我走這條路線,我將不得不重新計算屏幕旋轉的圖像視圖。我只是希望有一個簡單的解決方案,只使用XML。就像「將父母高度設置爲這個孩子的高度並忽略其他孩子一樣」謝謝! –

0

試着實現下面的代碼。

看到下面的圖片。

Here is the Screen shot of below code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:weightSum="1" 
      android:background="#FFFFFF" 
      android:layout_height="match_parent"> 

<LinearLayout 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:layout_weight="1" 
     android:background="#000000" 
     android:layout_marginTop="15dp" 
     android:layout_marginBottom="15dp" 
     android:layout_marginLeft="15dp" 
     android:layout_marginRight="15dp" 
     android:weightSum="5" 
     android:orientation="horizontal"> 

    <ImageView 
      android:layout_height="match_parent" 
      android:layout_width="match_parent" 
      android:layout_weight="0.9" 
      android:background="@drawable/black"/> 

    <LinearLayout 
      android:layout_height="match_parent" 
      android:layout_width="match_parent" 
      android:layout_weight="1.15"> 
    </LinearLayout> 
    <LinearLayout 
      android:layout_height="match_parent" 
      android:layout_width="match_parent" 
      android:layout_weight="0.9" 
      android:weightSum="4" 
      android:orientation="vertical"> 

     <TextView 
       android:layout_height="match_parent" 
       android:layout_width="match_parent" 
       android:layout_weight="1" 
       android:text="Linear Layout" 
       android:gravity="center_vertical|center_horizontal" 
       android:textSize="25dp"/> 
     <TextView 
       android:layout_height="match_parent" 
       android:layout_width="match_parent" 
       android:layout_weight="1" 
       android:text="TextView" 
       android:gravity="center_vertical|center_horizontal" 
       android:textSize="25dp"/> 
     <TextView 
       android:layout_height="match_parent" 
       android:layout_width="match_parent" 
       android:layout_weight="1" 
       android:text="TextView" 
       android:gravity="center_vertical|center_horizontal" 
       android:textSize="25dp"/> 
     <TextView 
       android:layout_height="match_parent" 
       android:layout_width="match_parent" 
       android:layout_weight="1" 
       android:text="TextView" 
       android:gravity="center_vertical|center_horizontal" 
       android:textSize="25dp"/> 
    </LinearLayout> 

<LinearLayout 
      android:layout_height="match_parent" 
      android:layout_width="match_parent" 
      android:layout_weight="1.15"> 
    </LinearLayout> 
    <LinearLayout 
      android:layout_height="match_parent" 
      android:layout_width="match_parent" 
      android:layout_weight="0.9" 
      android:weightSum="4" 
      android:orientation="vertical"> 
     <TextView 
       android:layout_height="match_parent" 
       android:layout_width="match_parent" 
       android:layout_weight="1" 
       android:text="Linear Layout" 
       android:gravity="center_vertical|center_horizontal" 
       android:textSize="25dp"/> 
     <TextView 
       android:layout_height="match_parent" 
       android:layout_width="match_parent" 
       android:layout_weight="1" 
       android:text="TextView" 
       android:gravity="center_vertical|center_horizontal" 
       android:textSize="25dp"/> 
     <TextView 
       android:layout_height="match_parent" 
       android:layout_width="match_parent" 
       android:layout_weight="1" 
       android:text="TextView" 
       android:gravity="center_vertical|center_horizontal" 
       android:textSize="25dp"/> 
     <TextView 
       android:layout_height="match_parent" 
       android:layout_width="match_parent" 
       android:layout_weight="1" 
       android:text="TextView" 
       android:gravity="center_vertical|center_horizontal" 
       android:textSize="25dp"/> 
    </LinearLayout> 
</LinearLayout> 

+5

您可以使用更好的圖像來演示......或者至少將背景改爲相反的顏色。 – Subby

相關問題