开发者

Is it possible in android to define constants in XML that vary with configuration

I am interested in having an XML layouts in Android change size depending on landscape or portrait viewing (and maybe other configurations later).

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:background="@drawable/stream_bg_1px_wide"
android:layout_height="150dip" >

In this example I just want to vary the 150dip开发者_运维技巧 size depending on landscape or portrait, not change anything else.

I am using layout and layout-land, and I know I could repeat the layout in each of those folders, but that makes maintaining changes in it a bit of a pain. When I introduce more variants based on screen density and size it just gets worse.

So, I wondered if it's possible to define a value (constant) in an XML file, and reference it from my layout, similar to how colors can be defined

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="shopper_background"> #FFFFFF</color>
</resources>

along the lines of......

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <value name="footer_size" >150</value>
</resources>

Then I could just replicate that file, with different values, in each config.

thanks for any help


Yes you can do this, we do it all over the place in Android itself :) Just define your constants in res/values/, res/values-land/ etc. For dimensions, use the tag and refer to them using @dimen/my_value in the layout.


Yes. A dimension is an accepted item in a resource file. So create e.g. res/values/dimension.xml and put

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


I will try to explain it shortly.

First, you may notice that now you should use ConstraintLayout as requested by google (see androix library).

In your android studio projet, you can provide screen-specific layouts by creating additional res/layout/ directories. One for each screen configuration that requires a different layout.

This means you have to use the directory qualifier in both cases :

  • Android device support
  • Android landscape or portrait mode

As a result, here is an exemple :

res/layout/main_activity.xml                # For handsets
res/layout-land/main_activity.xml           # For handsets in landscape
res/layout-sw600dp/main_activity.xml        # For 7” tablets
res/layout-sw600dp-land/main_activity.xml   # For 7” tablets in landscape

You can also use qualifier with res ressources files using dimens.xml.

res/values/dimens.xml                # For handsets
res/values-land/dimens.xml           # For handsets in landscape
res/values-sw600dp/dimens.xml        # For 7” tablets

res/values/dimens.xml

<resources>
    <dimen name="grid_view_item_height">70dp</dimen>
</resources>

res/values-land/dimens.xml

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

your_item_grid_or_list_layout.xml

<androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintlayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content

    <ImageView
            android:id="@+id/image"
            android:layout_width="0dp"
            android:layout_height="@dimen/grid_view_item_height"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:background="@drawable/border"
            android:src="@drawable/ic_menu_slideshow">

</androidx.constraintlayout.widget.ConstraintLayout>

Source : https://developer.android.com/training/multiscreen/screensizes

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜