简体   繁体   中英

How can I specify the layout_height value based on the layout_width in Android layout?

I am absolutly new in Android development and I have a problem positioning image in the first app that I am developing.

So I have the following situation.

Into a layout file I have:

<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>

So, as you can see, there is this ImageView element:

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

As you can see, for this ImageView I have setted the following values for the width and for the height

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

This because I want that the image occupies horizontally all the space, but I have also to put a vaue for the height.

So for the previous value I obtain the following result:

在此处输入图像描述

As you can see in the previous screenshot there is a space up and down the image because I am manually giving a specific value to my ImageView element and this is wrong.

I have tryed to set a lower hwight value and this empty space is deleted for the android:layout_height="240dp" value.

But I think that this is not a good solution because it depens from the used screen.

So, what is the best way to handle this situation?

I have an image view that horizontally have to fill the screen and its height have be automatically handled and not manually specified with a dp value.

How can I implement this behavior? What am I missing?

Your problem can be solved with this simple line of code below

The XML attribute for this is:

android:scaleType="fitCenter"

Put it here like this your code below I just added new line see #1

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

Also you are using src but you can use background attribute:

android:background="@drawable/carbonara"

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

you can use screen aspect ratio for setting height and width to the image.Try following function.` public void calculateAspectRatio() {

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;

`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM