【问题标题】:Avoid Android pushing items off-screen in layout?避免Android在布局中将项目推送到屏幕外?
【发布时间】: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 帖子,但到目前为止似乎没有任何效果。这只是我目前拥有的。

  1. 解决方案?
  2. 为什么文本在 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


【解决方案1】:

关于card_layout.xml 布局,看起来您根本没有对最顶部的TextView 进行任何垂直对齐。那里有android:gravity 参数,但它与android:layout_gravity 不同。它只设置视图的内容如何出现在视图,它不定位视图自身。您应该添加一行 `android:layout_alignTop="@id/image" 而不是重力。

至于第二个,您已将卡片的高度设置为与父级匹配,因此它填满整个屏幕也就不足为奇了。您需要将所有内容(包括布局和内部元素)的高度设置为 wrap_content 并删除 weight 属性,它们需要设置为所有视图才能执行任何有用的操作。

另一件重要的事情。我注意到您没有在ImageViews 上指定android:adjustViewBounds。您必须将其设置为true,否则视图将尝试设置与固有可绘制尺寸相关的比例,而不是缩放以保持纵横比。

关于学习材料,我会首先阅读线性和相对布局的一般工作原理,您的问题不一定与嵌套事物有关。现在网上有很多,我相信你可以自己找到它们。也不要害怕在 Android Studio 中进行实验。

【讨论】:

  • 谢谢,虽然我还是不太明白。也许是一个更具体的问题:为什么当我有一个带有 wrap_content 的高度和宽度和 scaleType:fitStart 的 ImageView 嵌套在一个在某个点具有固定大小的 Layout 中时,ImageView 的右侧是否有很大的空间?我原以为它将图像缩放到最大高度,然后具有图像的最大宽度+填充,而不是图像+填充+ ??...如果我明确设置宽度,它会完全按预期工作.
  • @Asmodean 我刚刚注意到你也没有在你的观点上指定android:adjustViewBounds,我已经更新了答案。希望对您有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-06
  • 2011-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-31
相关资源
最近更新 更多