我有兩個類似的問題,雙方圍繞涉及的ImageView
推動TextView
(S)和我想不出爲什麼。對於卡片列表,我希望圖像的高度與其父母相匹配,而寬度適當地縮放,並且文字左對齊圖像的右側。對於彈出窗口,請使用下面的文本將寬度縮放到父級和縮放高度。避免Android在佈局中將項目推出屏幕?
當前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帖子嘗試了幾種解決方案,但似乎沒有任何工作至今。這只是我目前所擁有的。
- 解決方案?
- 爲什麼文本被推到LinearLayout的底部而不是直接出現在底部?或者爲什麼它在和
layout_toRightOf
上TextView
上使用wrap_content
時在RelativeLayout
的屏幕上被推下?
關於#2,由於類似問題上的答案只針對特定的問題發佈解決方案,沒有任何解釋,我似乎無法將其應用於我的情況。我正在尋找一種資源來理解佈局和視圖如何相互作用(單個和什麼時候嵌套),如果解釋太長的答案。
您應該使用嵌套的佈局和使用權重屬性:) – HelloSadness
對於彈出,請嘗試在'ImageView'取出'layout_weight',其'layout_height'設置爲'wrap_content'。 – clownba0t
關於該卡,我在本地嘗試了您的佈局,併爲我工作。只是爲了確認,文本視圖是否可見,使用白色以外的字體顏色,並且它們應該以編程方式正確顯示文本? – clownba0t