android - Dp unit does not scale layouts in different screens -


after reading (http://developer.android.com/guide/practices/screens_support.html), have developed entire app using dp unit inside xml files. however, when test app in different screens, layouts either big or small.

i thought dp unit fix me. why didn't it? not want use weight attribute since done.

one xml layout:

<imageview     android:layout_width="match_parent"     android:layout_height="140dp"     android:src="@drawable/logo3"     android:scaletype="centercrop"      />  <textview     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="@string/select_level"     android:textcolor="#4cb122"     android:layout_gravity="center_horizontal"     android:gravity="center_horizontal"     android:textsize="20dp"     android:layout_margintop="20dp"     />  <button     android:background="@drawable/red_button"     android:layout_width="200dp"     android:layout_height="55dp"     android:layout_gravity="center_horizontal"     android:layout_margintop="25dp"     android:text="@string/easy"     android:textsize="15dp"     android:onclick="playeasy"     style="custom_button"     />  <button     android:background="@drawable/green_button"     android:layout_width="200dp"     android:layout_height="55dp"     android:layout_gravity="center_horizontal"     android:layout_margintop="10dp"     android:text="@string/medium"     android:textsize="15dp"     android:onclick="playmedium"     style="custom_button"     />  <button     android:background="@drawable/blue_button"     android:layout_width="200dp"     android:layout_height="55dp"     android:layout_gravity="center_horizontal"     android:layout_margintop="10dp"     android:textsize="15dp"     android:text="@string/unbeatable"     android:onclick="playunbeatable"     style="custom_button"     />  <textview     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:gravity="center_horizontal"     android:layout_margintop="113dp"     android:textsize="15dp"     android:textcolor="@color/secondtextcolor"     android:text="@string/developed_by"     /> 

what do? thanks!

using dp dimensions not 1 size fits solution problem.

note should use layout weight when possible, , in general 1 dp value should work screen sizes. however, run in edge case causes problems, , need make work (for example had use technique positioning badge on tab in tablayout correctly screen sizes).

what around put dimens.xml file each supported screen size:

  • res/values-small/dimens.xml

  • res/values-normal/dimens.xml

  • res/values-large/dimens.xml

  • res/values-xlarge/dimens.xml

you can use other qualifiers target tablets if needed, see here guide configuration qualifier names.

then, specify each dimension each screen size qualifier in each file (note needs done dimension values causing problems on large or small screens).

for example in res/values-large/dimens.xml might have this:

<?xml version="1.0" encoding="utf-8"?> <resources>      <dimen name="image_view_height">140dp</dimen>  </resources> 

then in res/values-small/dimens.xml might have make fit on smaller screens:

<?xml version="1.0" encoding="utf-8"?> <resources>      <dimen name="image_view_height">96dp</dimen>  </resources> 

then, in layout, reference @dimen/your_dimens_id, , framework choose correct 1 take screen size of device:

<imageview     android:layout_width="match_parent"     android:layout_height="@dimen/image_view_height"     android:src="@drawable/logo3"     android:scaletype="centercrop"      /> 

Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

json - Gson().fromJson(jsonResult, Myobject.class) return values in 0's -