【问题标题】:Android fitting image to only needed spaceAndroid将图像拟合到只需要的空间
【发布时间】:2017-08-04 22:46:25
【问题描述】:

我想要显示一张图片,上面有文字,下面有一行。我希望图像使用所提供空间的宽度,然后高度刚好足以不浪费空间。当前的 XML 设置是:

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

    <TextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textStyle="normal"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:text="" />
     <ImageView 
        android:id="@+id/photoStatic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding ="5dp"
        android:layout_gravity="center"
        android:layout_weight="0"/>
    <View
        android:layout_marginTop="8dp"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/grey" />

</LinearLayout>

这是它最终的样子:

我想消除图像下方和上方的空白空间,以便完美地调整图像大小。我曾尝试修改android:layout_heightandroid:layout_width,但没有取得多大成功。

【问题讨论】:

    标签: android image android-linearlayout android-wrap-content


    【解决方案1】:

    属性android:adjustViewBounds="true" 应该做的事情。试试这个:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    
        <TextView
            android:id="@+id/name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:text="spam spam spam"
            android:textSize="18sp"
            android:textStyle="normal" />
    
        <ImageView
            android:id="@+id/photoStatic"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true" />
    
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginTop="8dp"
            android:background="@color/grey" />
    
    </LinearLayout>
    

    【讨论】:

    • 就像一个魅力。我将不得不用一些不同尺寸的图像验证一些不同的分辨率,但我认为我们有一个赢家!谢谢!
    【解决方案2】:

    只需删除 layout_weight。我认为就足够了,并且还使用 Space 而不是 View (android.support.v4.widget.Space)。还在使用 fill_parent?同名是 match_parent。不再使用 fill_parent。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-28
      • 1970-01-01
      • 2016-08-18
      • 1970-01-01
      • 2017-06-14
      • 2014-04-06
      • 2011-08-26
      • 1970-01-01
      相关资源
      最近更新 更多