How to make layout_height and layout_width with equal size

I have an image with a resolution of 320x320.

To layout_widthset the valuefill_parent

but I don’t know what I need to install for layout_height

I want to have the same size in height and width.

If I set layout_height=wrap_content, the width will be twice as large as the height ...

How can I increase some layout to fill all the space in x, but have size y just like size x

+5
source share
4 answers
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

int width = metrics.widthPixels;

Now dynamically set the height using the width.

this is the width of the device screen since you are using layout_width = fill parent.

+2
source

You tried to explicitly set layout_height

eg,

android:layout_height="320dip"
+1
source

onCreate(...), setContentView(...);

:

ImageView image = (ImageView)findViewById(R.id.image);
image.getLayoutParams().width = image.getHeight();
+1

layout_height = wrap_content, , ...

, , , / WRAP_CONTENT, , , , ,

FILL_PARENT, .

  • , dp, , XML .
  • LinearLayout weightSum layout_weight , . [ weight]
+1

All Articles