【问题标题】:LinearLayout ImageView pushes views off screen?LinearLayout ImageView 将视图推离屏幕?
【发布时间】:2017-11-03 05:49:42
【问题描述】:

所以对于我的生活,我似乎无法弄清楚似乎是一个简单的布局问题。我希望也许一双新的眼睛可以帮助我。

问题:我在垂直 LinearLayout 中有三个视图。

  • 一个文本视图
  • ImageView 需要在 AdjustViewBounds 设置为 true 的情况下保持其纵横比并包装内容。 (如果将布局权重设置为 ImageView,则纵横比似乎被破坏了)
  • 另一个文本视图

我不能使用自定义视图,因为这是用于 Widget。

问题是当可用空间太小时 ImageView 将底部 TextView 推到屏幕之外,我正在寻找的预期结果是 ImageView 调整大小以适应两个 TextView 将它们都保留在内部尽可能长的观看区域。我可以通过将布局权重设置为 ImageView 来获得所需的效果,但由于某种原因,这会破坏纵横比。

左侧的预期结果。但是,当尺寸太高时会丢失纵横比。

守则:

<?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"
    android:gravity="center">
    <TextView
        android:paddingBottom="8dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textStyle="bold"
        android:textSize="12sp"
        android:text="Text"/>

    <ImageView
        android:contentDescription="@string/app_name"
        android:src="@drawable/sunny"
        android:scaleType="fitXY"
        android:cropToPadding="true"
        android:adjustViewBounds="true"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"/>

    <TextView
        android:paddingTop="8dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textStyle="bold"
        android:textSize="12sp"
        android:text="Text"/>
</LinearLayout>

示例:

TextView 被推出屏幕,而 ImageView 没有调整大小以适应它:

ImageView 设置为layout_weight="1" 和layout_height="0dp"。现在它会根据需要调整大小以适应底部 TextView,但会丢失其纵横比:

ImageView 设置为layout_weight="1"、layout_height="0dp" 和scaleType="fitCenter"。这保留了纵横比,同时仍然调整大小以适应底部的 TextView。但是有多余的空白,TextView 不再靠近图像。

任何帮助将不胜感激。希望我充分解释了这个问题。谢谢你:)!

内森

【问题讨论】:

  • 你需要展示什么?没有完全从你的问题中得到。请详细说明。

标签: android android-layout layout imageview


【解决方案1】:

试试这样的:

<?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"
    android:weightSum="100"
    android:gravity="center">
    <TextView
        android:paddingBottom="8dp"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="20"
        android:textColor="#000000"
        android:textStyle="bold"
        android:textSize="12sp"
        android:text="Text"/>

    <ImageView
        android:contentDescription="@string/app_name"
        android:src="@drawable/sunny"
        android:scaleType="fitXY"
        android:cropToPadding="true"
        android:adjustViewBounds="true"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="60"/>

    <TextView
        android:paddingTop="8dp"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="20"
        android:textColor="#000000"
        android:textStyle="bold"
        android:textSize="12sp"
        android:text="Text"/>
</LinearLayout>

【讨论】:

  • 您好,感谢您的回复!不幸的是,这会产生类似于我帖子中第二张图片的内容。纵横比没有保留,顶部的 TextView 与实际图像之间有很多额外的空白。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-02
  • 1970-01-01
  • 1970-01-01
  • 2017-08-17
  • 1970-01-01
相关资源
最近更新 更多