How to setup same ImageView size on different screens?

时间:2016-12-02 05:21:25

标签: android android-layout android-imageview dimensions

I was hoping that DP pixels are solving issue of different screen sizes.

I've set up my ImageView as this:

<ImageView
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:src="@drawable/center_ring"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"/>

Then I tried to launch this app on virtual devices of Nexus 5X and Nexus S with different screen sizes. As you can see relative size of ImageView is quite different.

How do I get same relative size on different screens?

enter image description here enter image description here

2 个答案:

答案 0 :(得分:1)

try using this library for your dimension. this will help you overcome your issues but its not a ideal solution , ideally i would recommend you to use constraint layout or layout weights https://github.com/intuit/sdp

This shall help you get screen size

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

答案 1 :(得分:0)

You can do it by giving width and height as px instead dp. For same relative size you can use layout weight for used and un-used area. It'll solve your problem.

Edit Then you should have custom views and create all views as per weight calculations. Then only you'll achieve this.