【问题标题】:ImageView inside LinearLayout is having margins on sides by default默认情况下,LinearLayout 内的 ImageView 在侧面有边距
【发布时间】:2019-07-05 21:42:56
【问题描述】:

我想知道,仍然找不到这个问题。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <FrameLayout
            android:background="@color/colorPrimary"
            android:layout_width="match_parent"
            android:layout_height="48dp"/>

    <ImageView
            android:src="@drawable/someImage"
            android:id="@+id/kioskModeImageView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

</LinearLayout>

对于上述结构,我的 ImageView 两侧被缩短,左右两边有一些奇怪的边距,我不知道它们是从哪里来的。

当我从容器中的 LinearLayout 中删除 FrameLayout(并且仅将 ImageView 作为一件事)时,一切都按预期工作 - 图像占据了整个位置,因为 match_parent 应该可以工作

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <ImageView
            android:src="@drawable/customer_dark_1"
            android:id="@+id/kioskModeImageView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

</LinearLayout>

是什么导致了这个问题?

【问题讨论】:

    标签: android android-layout android-linearlayout android-imageview


    【解决方案1】:

    您尚未将 scaleType 设置为 imageview,尝试设置 android:scaleType="fitXY" scaleType 有其他值,尝试使用适合您要求的值。

    【讨论】:

      【解决方案2】:

      在您的情况下,match_parentmatch_parent 不适用于带有FrameLayout 的图像。 您需要添加 layout_weight 并设置 layout_height="0dp" 以使其看起来像预期的那样

      <LinearLayout
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
      
          <FrameLayout
                  android:background="@color/colorPrimary"
                  android:layout_width="match_parent"
                  android:layout_height="48dp"/>
      
          <ImageView
                  android:src="@drawable/someImage"
                  android:id="@+id/kioskModeImageView"
                  android:layout_width="match_parent"
                  android:layout_height="0dp"
                  android:layout_weight="1" />
      
      </LinearLayout>
      

      另外,在这篇文章中解释了ImageView scaleType

      https://thoughtbot.com/blog/android-imageview-scaletype-a-visual-guide

      【讨论】:

        猜你喜欢
        • 2021-05-31
        • 1970-01-01
        • 2015-11-26
        • 2023-03-25
        • 2017-03-20
        • 1970-01-01
        • 1970-01-01
        • 2020-01-13
        • 2012-02-29
        相关资源
        最近更新 更多