简体   繁体   中英

How to properly use different dimens resource in different Android screens

I have 4 different values folder in my res folder with different dimens.xml inside it. Each dimens.xml file have different values. The folders i got was the following:

values/dimens.xml
values-sw320dp/dimens.xml
values-sw480dp/dimens.xml
values-sw600dp/dimens.xml

The problem is when I run my app, it seems that the value used is the dimens.xml values stored in values-sw320dp/dimens.xml even when I run it in my Google Nexus 5 emulator.

Example usage is this:

<TextView
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_gravity="center_vertical"
     android:text="Account "
     android:textSize="@dimen/common_textSize_n" />

What am I doing wrong? Or what is the proper way to use different dimens values for different screen sizes?

Any help would be greatly appreciated. Thank you very much!

I apologize that this question seems have multiple duplicates. I just didn't find the answer to the already asked questions.

You have to put different value in each folder.and dimens.xml will be the default one,if any of the dimension is missing it will take form there. and all other cases it will take the values according to the screen size. what you are doing is correct ,also you can add more dimens for mdpi,xhdpi,hdpi,xhdpi etc.

Rename the folder names to values-w max_size dp

Eg :-

  • values-w320dp
  • values-w600dp
  • values-w820dp

Every folder will have one file namely dimens.xml. Lets say an example if you have "feed_title_size" dimen, using in any layout file. You want to set the textSize with this dimen but for different screen resolutions. then you need to create dimens.xml file in every resolution folder(eg values-w320dp) and create a dimen attribute in it with "feed_title_size" name inside the resources tag. So "feed_title_size" will present in multiple dimens.xml file and the appropriate value for it will be selected automatically on the basis of the screen resolution.

Here is the dimens.xml in values-w320dp folder looks like:

<resources>
    <dimen name="feed_name_handle_width">100dp</dimen>
</resources>

Here is the dimens.xml in values-w640dp folder looks like:

<resources>
    <dimen name="feed_name_handle_width">150dp</dimen>
</resources>

Here is the original dimens.xml file which contains in values folder. This file is default file.

<resources>
    <dimen name="feed_text_width">50dp</dimen>
</resources>

Here is your layout file which contains your TextView

<android.support.v7.widget.AppCompatTextView
    android:layout_width="wrap_parent"
    android:layout_height="wrap_content"
    android:textSize="@dimen/feed_text_width" />

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