Java 如何根据Android布局中的布局宽度指定布局高度值?

Java 如何根据Android布局中的布局宽度指定布局高度值?,java,android,android-layout,android-imageview,android-styles,Java,Android,Android Layout,Android Imageview,Android Styles,我是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">

我是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>
这是因为我希望图像水平地占据所有空间,但我还必须为高度设置一个vaue

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

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

我尝试设置一个较低的hwight值,并且删除了android:layout_height=“240dp”值的空白

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

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

我有一个图像视图,水平方向必须填充屏幕,其高度自动处理,而不是手动指定dp值


如何实现此行为?我遗漏了什么?

尝试使用android:scaleType=“centerCrop”或
android:scaleType=“fitXY”
如果你需要重新缩放图片,朋友,我完全理解你的问题,我也对这些烦人的事情感到困惑,但现在我学到了很多真正有用的东西。。 因此,您的问题可以通过下面这行简单的代码来解决

此文件的XML属性是

android:scaleType=“fitCenter”

把它放在这里,就像下面的代码一样。我刚刚添加了新行,请参见#1


我希望我已经解释清楚了。如果这有助于你做这个答案。 也可以使用src,但可以使用background属性
android:background=“@drawable/carbonara”

您可以使用屏幕纵横比设置图像的高度和宽度。请尝试以下功能 public void calculateAspectRatio(){


尝试将android:scaleType=“fitXy”或fitCenter放到ImageView中。你可以选择任何高度比,比如4/3 Hanks Balaji,这是一个有用的答案,我试过了,它有效:)
<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="300dp"
    android:src="@drawable/carbonara" />
android:layout_width="wrap_content"
android:layout_height="300dp
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:scaleType="fitCenter"             // #1
        android:layout_height="300dp"
        android:src="@drawable/carbonara" />
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;

`