1

我有兩個類似的問題,雙方圍繞涉及的ImageView推動TextView(S)和我想不出爲什麼。對於卡片列表,我希望圖像的高度與其父母相匹配,而寬度適當地縮放,並且文字左對齊圖像的右側。對於彈出窗口,請使用下面的文本將寬度縮放到父級和縮放高度。避免Android在佈局中將項目推出屏幕?

Current CardLayout output Current CardInfoLayout output

我很期待/尋找:

enter image description here enter image description here

當前card_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="120dp" > 

<android.support.v7.widget.CardView 
    android:id="@+id/card_view" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_margin="10dp" 
    android:layout_height="120dp" 
    card_view:cardCornerRadius="4dp" 
    card_view:elevation="14dp"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <ImageView 
      android:id="@+id/image" 
      android:layout_height="match_parent" 
      android:layout_width="wrap_content" 
      android:scaleType="fitStart" 
      android:padding="4dp" 
      android:layout_gravity="left" 
      android:layout_weight="1"> 
     </ImageView> 

     <TextView 
      android:id="@+id/location" 
      android:textSize="16dip" 
      android:layout_toRightOf ="@+id/image" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 

      android:gravity="top" > 
     </TextView> 

     <TextView 
      android:id="@+id/abbreviation" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/location" 
      android:layout_toRightOf="@+id/image" 
      android:layout_toEndOf="@+id/image"> 
     </TextView> 

    </RelativeLayout> 
</android.support.v7.widget.CardView> 

</RelativeLayout> 

當前location_info_card.xml:

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

<ImageView 
    android:id="@+id/info_image" 
    android:layout_height="0dp" 
    android:layout_width="match_parent" 
    android:scaleType="fitStart" 
    android:padding="10dp" 
    android:layout_weight="1"> 
</ImageView> 

<TextView 
    android:id="@+id/info_abbreviation" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="20sp" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp" 
    android:paddingBottom="4dp"> 
</TextView> 

<TextView 
    android:id="@+id/info_location" 
    android:textSize="16sp" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp" 
    android:paddingBottom="4dp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
</TextView> 
</LinearLayout> 

我從教程以及與單一和嵌套佈局SO帖子嘗試了幾種解決方案,但似乎沒有任何工作至今。這只是我目前所擁有的。

  1. 解決方案?
  2. 爲什麼文本被推到LinearLayout的底部而不是直接出現在底部?或者爲什麼它在和layout_toRightOfTextView上使用wrap_content時在RelativeLayout的屏幕上被推下?

關於#2,由於類似問題上的答案只針對特定的問題發佈解決方案,沒有任何解釋,我似乎無法將其應用於我的情況。我正在尋找一種資源來理解佈局和視圖如何相互作用(單個和什麼時候嵌套),如果解釋太長的答案。

+0

您應該使用嵌套的佈局和使用權重屬性:) – HelloSadness

+0

對於彈出,請嘗試在'ImageView'取出'layout_weight',其'layout_height'設置爲'wrap_content'。 – clownba0t

+0

關於該卡,我在本地嘗試了您的佈局,併爲我工作。只是爲了確認,文本視圖是否可見,使用白色以外的字體顏色,並且它們應該以編程方式正確顯示文本? – clownba0t

回答

0

關於card_layout.xml佈局,它看起來像你根本沒有任何垂直對齊的最頂端TextView。您在那裏有android:gravity參數,但它與android:layout_gravity不一樣。它只設置視圖的內容如何顯示來看,它並不定位視圖本身。你應該添加一行像android:layout_alignTop =「@ id/image」而不是重力。

至於第二個,你已經設置了卡的高度以匹配父母,所以沒有什麼奇怪的是它填滿了整個屏幕。您需要將所有內容(佈局和內部元素)的高度設置爲wrap_content並刪除重量屬性,它們需要設置爲所有視圖以執行任何有用的操作。其他

一個重要的事情。我注意到你沒有在你的ImageView上指定android:adjustViewBounds。您必須將其設置爲true,否則視圖將嘗試設置與內在可繪製大小相關的比例,而不是比例以保持寬高比。

關於學習資料,我會先了解線性和相對佈局的工作原理,首先,您的問題不一定與嵌套的東西有關。到目前爲止,網絡上有很多人,我相信你可以自己找到它們。在Android Studio中也不要害怕嘗試。

+0

謝謝,雖然我還是不太明白。也許是一個更具體的問題:爲什麼當我有一個帶有用於height&width和scaleType的wrap_content的ImageView時:fitStart嵌套在佈局中,在某個點具有固定大小時,ImageView是否在它的右邊有一個巨大的空間?我會希望它會將圖像縮放到最大高度,然後獲得圖像+填充的最大寬度,而不是圖像+填充+ ?? ......如果我明確設置了寬度,則它完全按照預期工作。 – Asmodean

+0

@Asmodean我剛剛注意到你也沒有在你的視圖中指定'android:adjustViewBounds',我已經更新了答案。希望有所幫助。 – Malcolm