android中是否可以在XML中定义随配置而变化的常量

发布于 2024-10-21 18:59:10 字数 930 浏览 1 评论 0原文

我感兴趣的是 Android 中的 XML 布局可以根据横向或纵向查看(以及稍后的其他配置)更改大小。

<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" >

在此示例中,我只是想要根据横向或纵向改变 150dip 大小,而不改变其他任何内容。

我正在使用布局和布局土地,并且我知道我可以在每个文件夹中重复布局,但这使得维护其中的更改有点痛苦。当我根据屏幕密度和尺寸引入更多变体时,情况只会变得更糟。

所以,我想知道是否可以在 XML 文件中定义一个值(常量),并从我的布局中引用它,类似于如何

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

沿着......

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

定义颜色。然后我可以复制该文件,在每个配置中具有不同的值。

感谢您的帮助

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

好多鱼好多余 2024-10-28 18:59:10

是的,你可以这样做,我们在 Android 本身中到处都这样做:) 只需在 res/values/、res/values-land/ 等中定义常量。对于尺寸,请使用标签并使用 @dimen/ 引用它们布局中的 my_value。

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.

宣告ˉ结束 2024-10-28 18:59:10

是的。维度是资源文件中可接受的项目。因此,创建例如 res/values/dimension.xml 并放入

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

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>
流云如水 2024-10-28 18:59:10

我会尽快解释它。

首先,您可能会注意到,现在您应该使用 ConstraintLayout 作为谷歌请求(参见androix库)。

在您的 android studio 项目中,您可以通过创建额外的 res/layout/ 目录来提供特定于屏幕的布局。每个需要不同布局的屏幕配置都有一个。

这意味着您必须在两种情况下使用目录限定符

  • Android 设备支持
  • Android 横向或纵向模式

因此,这里是一个示例:

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

您还可以使用 dimens.xml 对 res 资源文件使用限定符。

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>

来源: https ://developer.android.com/training/multiscreen/screensizes

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文