【问题标题】:Is it possible in android to define constants in XML that vary with configuration是否可以在 android 中定义随配置变化的 XML 常量
【发布时间】:2011-03-12 20:56:05
【问题描述】:

我有兴趣让 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 的大小,而不是改变其他任何东西。

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

所以,我想知道是否可以在 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>

然后我可以在每个配置中复制具有不同值的文件。

感谢您的帮助

【问题讨论】:

    标签: android xml layout constants


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        我会尽快解释。

        首先,您可能会注意到现在您应该按照 google 的要求使用 ConstraintLayout(请参阅 android 库)。

        在您的 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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-10-11
          • 2014-07-26
          • 1970-01-01
          • 2012-04-29
          • 1970-01-01
          • 1970-01-01
          • 2015-09-27
          • 1970-01-01
          相关资源
          最近更新 更多