【问题标题】:How can I specify the layout_height value based on the layout_width in Android layout?如何根据 Android 布局中的 layout_width 指定 layout_height 值?
【发布时间】:2016-06-12 18:22:58
【问题描述】:

我是 Android 开发的新手,在我正在开发的第一个应用程序中定位图像时遇到问题。

所以我有以下情况。

放入我拥有的布局文件中:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Dummy content. -->
    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="0dp">

       <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="300dp"
            android:src="@drawable/carbonara" />

        <TextView android:id="@android:id/text1"
            style="?android:textAppearanceLarge"
            android:textStyle="bold"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp" />

    </LinearLayout>

</ScrollView>

所以,如您所见,有这个 ImageView 元素:

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="300dp"
    android:src="@drawable/carbonara" />

如您所见,对于这个 ImageView,我为宽度和高度设置了以下值

android:layout_width="wrap_content"
android:layout_height="300dp

这是因为我希望图像水平占据所有空间,但我还必须为高度设置一个值。

所以对于之前的值,我得到以下结果:

正如您在上一个屏幕截图中看到的那样,图像上下有一个空格,因为我手动为 ImageView 元素指定了一个特定值,这是错误的。

我已尝试设置较低的 hwight 值,并为 android:layout_height="240dp" 值删除了这个空白空间。

但我认为这不是一个好的解决方案,因为它取决于使用的屏幕。

那么,处理这种情况的最佳方法是什么?

我有一个图像视图,它必须水平填充屏幕,并且它的高度已自动处理,而不是手动指定 dp 值。

如何实现这种行为?我错过了什么?

【问题讨论】:

  • 尝试把 android:scaleType="fitXy" 或 fitCenter 放到 ImageView 中。

标签: java android android-layout android-imageview android-styles


【解决方案1】:

朋友,我完全理解你的问题,我也对这些烦人的事情感到困惑,但现在我学到了很多真正有帮助的东西.. 所以你的问题可以用下面这行简单的代码来解决

这个的 XML 属性是...

android:scaleType="fitCenter"

像这样把你的代码放在这里我刚刚添加了新行,请参阅#1

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:scaleType="fitCenter"             // #1
        android:layout_height="300dp"
        android:src="@drawable/carbonara" />

我希望我已经解释清楚了。如果它帮助你支持这个答案。 你也使用 src 但你可以使用背景属性 android:background="@drawable/carbonara"

【讨论】:

    【解决方案2】:

    如果需要重新缩放图片,请尝试使用android:scaleType="centerCrop"android:scaleType="fitXY"

    【讨论】:

      【解决方案3】:

      您可以使用屏幕纵横比来设置图像的高度和宽度。尝试以下功能。` 公共无效计算AspectRatio(){

      int imagewidth;
      int imagewidth;
      DisplayMetrics displaymetrics = new DisplayMetrics();
              getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);  
                   int height = displaymetrics.heightPixels;
                  int width = displaymetrics.widthPixels;
      
                   imagewidth=width;
                  imageHeight = (imageTwoWidth / 4) * 3;
      }
      
      set image height and width
      
                  imageview.requestLayout();
                  imageview.getLayoutParams().width = imagewith;//take value from above function for height and width.
                  imageview.getLayoutParams().height =imageheight;
      
      `
      

      【讨论】:

      • 你可以任意比例的高度,比如 4/3
      • 谢谢巴拉吉,有用的答案,我试过了,它有效:)
      猜你喜欢
      • 2016-05-19
      • 2011-06-28
      • 2012-01-31
      • 2016-04-24
      • 1970-01-01
      • 1970-01-01
      • 2015-04-07
      • 2014-11-21
      相关资源
      最近更新 更多