Home > Blockchain >  Is there a way to apply a multiplier to all dimensions within a layout file?
Is there a way to apply a multiplier to all dimensions within a layout file?

Time:11-05

I currently have two xml files per component to support normal and large scale devices. However, for most of these files all i'm doing in the large bucket is copying over the normal size bucket and multiplying all dimensions by approximately 1.5.

For example:

activity_main.xml:

<include layout="@layout/help_popup"/>

activity_main.xml (large):

<include layout="@layout/help_popup"/>

help_popup.xml:

<TextView
 android:text_size = 30dp
/>

help_popup.xml (large):

<TextView
 android:text_size = 45dp
/>

Should become something like:

activity_main.xml:

<include layout="@layout/help_popup"/>

activity_main.xml (large):

<include layout="@layout/help_popup"
 android:multiply_dimensions = 1.5
/>

help_popup.xml:

<TextView
 android:text_size = 30dp
/>

help_popup.xml (large)

I'm using an extra layout file for large scale right now. I've googled and browsed stackoverflow, and I'm not sure if a solution to my question exists, but I'd like to make sure.

CodePudding user response:

You should be able to achieve this by using dimension resource files for normal and large sizes, rather than having layout resource files for normal and large sizes. You then don't need a large version of the help_popup and activity_main, since the normal and large dimens will set the help_popup TextView's text size accordingly. Like this:

dimens.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="help_popup_text_size">30sp</dimen>
</resources>

dimens.xml (large):

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="help_popup_text_size">45sp</dimen>
</resources>

help_popup.xml:

<TextView android:textSize="@dimen/help_popup_text_size"/>

activity_main.xml:

<include layout="@layout/help_popup"/>
  • Related