Android Layout - ImageView extension to fill width, but no more?

Hi, useful members of stackoverflow,

I am trying to accomplish what seems like a simple layout in Android activity, but I can't get it to work using only wrap_content and fill_parent / match_parent.

Image to give a general idea of โ€‹โ€‹the problem: http://i89.photobucket.com/albums/k207/cephron/Layout_quandary.png

In a vertical LinearLayout, I have an ImageView at the top of a child layout containing buttons and stuff. I would like the ImageView to be expanded to make the image as large as possible (i.e. to fill the width of the screen), without taking up more vertical space than is necessary to achieve this (i.e. I want the ImageView to touch the top of the screen, with buttons and stuff touch the bottom of the image). I can't make this happen using wrap_content and fill_parent, and all of my gravity mess seems to change nothing. I had to turn on the scales only so that the image did not completely knock everything off the screen.

I would like to publish the correct code, but - the noob that I am - I spent about 20 minutes trying to get it to work with Stackoverflow code formatting, and parts of it will not appear.

So, here is my code, distorted enough that it is not recognized by selectively deleting parts of it:

[LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_horizontal"] [ImageView android:id="@+id/door_detail_video_frame" android:src="@drawable/door_closed_256" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"/] [LinearLayout android:id="@+id/door_detail_control_area" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0"] [ImageView android:id="@+id/door_detail_status_frame" android:src="@drawable/door_closed_locked_67" android:layout_width="wrap_content" android:layout_height="fill_parent"/] [LinearLayout android:layout_height="wrap_content" android:orientation="vertical" android:layout_width="fill_parent"] [Button android:id="@+id/door_detail_override_button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/door_detail_override_button_text"/] [Button android:id="@+id/door_detail_camera_button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/door_detail_camera_button_text"/] [/LinearLayout] [/LinearLayout] [/LinearLayout] 

I believe that the only relevant parts are the first LinearLayout and ImageView, but the material for nested layouts (corresponding to โ€œbuttons and the likeโ€) is included for completeness.

Is there a way to achieve the desired layout formatting?

Thanks in advance for your help!

+4
source share
1 answer

Unconfirmed, this may help. I've tried things like this before, it seems more complicated than it should be:

 ImageView iv = (ImageView)findViewById(R.id.door_detail_video_frame); ViewTreeObserver vto = iv.getViewTreeObserver(); //gets screen dimensions DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); //this happens before the layout is visible vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { int newWidth, newHeight, oldHeight, oldWidth; //the new width will fit the screen newWidth = metrics.widthPixels; //so we can scale proportionally oldHeight = iv.getDrawable().getIntrinsicHeight(); oldWidth = iv.getDrawable().getIntrinsicWidth(); newHeight = Math.floor((oldHeight * newWidth) / oldWidth); iv.setLayoutParams(new LinearLayout.LayoutParams(newWidth, newHeight)); iv.setScaleType(ImageView.ScaleType.CENTER_CROP); //so this only happens once iv.getViewTreeObserver().removeGlobalOnLayoutListener(this); } }); 

Tip on formatting SO code: just copy and paste your code into the window, select all of it and press Ctrl + K.

+8
source

Source: https://habr.com/ru/post/1340689/


All Articles