【发布时间】:2016-11-12 11:04:29
【问题描述】:
我有两个类似的问题,都涉及ImageView 推动TextView(s),我不知道为什么。对于卡片列表,我希望图像的高度与其父级匹配,而宽度适当缩放,并且文本左对齐图像的右侧。对于弹出窗口,将宽度调整到父级并使用下面的文本缩放高度。
当前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>
当前位置_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 中被推到底部而不是直接出现在下方?或者为什么在
ImageView上使用wrap_content和在TextView上使用layout_toRightOf时,它会在RelativeLayout中从屏幕上推出?
关于#2,由于关于 SO 上类似问题的答案仅发布针对该问题的解决方案而没有任何解释,我似乎无法将其应用于我的情况。如果解释太长,我正在寻找一种资源来了解布局和视图如何交互(单个和嵌套时)。
【问题讨论】:
-
你应该使用嵌套布局并使用权重属性:)
-
对于弹出窗口,请尝试删除
ImageView上的layout_weight,并将其layout_height设置为wrap_content。 -
关于卡片,我在本地尝试了你的布局,它对我有用。只是为了确认一下,文本视图是否绝对可见,使用白色以外的字体颜色,并以编程方式正确设置它们应该显示的文本?
-
@clownba0t 是的,如果我更改布局类型/缩放比例以使所有项目出现在同一行上,那么每张卡片都可以看到正确的文本。
标签: android android-layout android-linearlayout android-view android-relativelayout