Android:如何使用xml字符串值来确定布局方向

odi*_*ity 7 android

我有一个简单的线性布局,我想根据设备的屏幕尺寸进行更改.我想要做的就像是

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="@string/cover_orientation"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>
Run Code Online (Sandbox Code Playgroud)

我在values和values-xlarge下创建了不同的dimen.xml文件,这样cover_orientation变量将根据屏幕大小采用不同的值("vertical"或"horizo​​ntal").

string name="cover_orientation">"vertical"
Run Code Online (Sandbox Code Playgroud)

但这不起作用.我找到了一个临时工作,包括检查屏幕大小和手动更改方向:

if(getResources().getString(R.string.screen_size) == "xlarge"){
    ((LinearLayout)convertView).setOrientation(1);
}
Run Code Online (Sandbox Code Playgroud)

但似乎你应该能够以第一种方式做到这一点(更优雅/更少的代码).

我认为每种屏幕尺寸都有不同的布局,但布局实际上非常大,这是我对不同屏幕尺寸所需的唯一改变.因此复制整个布局对我来说没有多大意义.

谢谢!

Mac*_*ski 10

一个很好的方法是添加

android:orientation="@integer/cover_orientation"
Run Code Online (Sandbox Code Playgroud)

在你的LinearLayout和定义它如下.

values/consts.xml:

<resources>

    <integer name="orientation_horizontal">0</integer>
    <integer name="orientation_vertical">1</integer>

</resources>
Run Code Online (Sandbox Code Playgroud)

values/something.xml:

<resources>

    <integer name="cover_orientation">@integer/orientation_vertical</integer>

</resources>
Run Code Online (Sandbox Code Playgroud)

values-land/something.xml:

<resources>

    <integer name="cover_orientation">@integer/orientation_horizontal</integer>

</resources>
Run Code Online (Sandbox Code Playgroud)

通过这种方式,您可以避免在应用程序中的方向变量定义中使用零编码和零编码.

  • @AHelloWorldDev这里有记录:https://developer.android.com/reference/android/R.attr.html#orientation,所以我认为这是非常未来的证据. (3认同)
  • 好的解决方案 但是,它是否有文件记录(或面向未来)? (2认同)

odi*_*ity 8

我最终通过浏览android文档找到了解决问题的方法.我最初拥有的是一个LinearLayout,其中包含一个图像内部的文本(其中实际上有更多的内容,但我会在这个例子中保持简单),看起来像这样:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<ImageView android:id="@+id/cover"
        android:layout_width="@dimen/cover_width"
        android:layout_height="@dimen/cover_height"/>

<TextView android:id="@+id/title"
        android:gravity="top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="@dimen/title_font_size"
        android:scrollHorizontally="true"
        android:maxLines="1"
        android:ellipsize="end" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我想要的是能够根据设备屏幕大小动态更改文本是在图像旁边还是在图像下面.换句话说,我想动态地动态改变android:orientation.我在我的问题中发布的第一个想法是在res/values/dimen.xml中声明一个字符串变量as

<string name="orientation">horizontal</string>
Run Code Online (Sandbox Code Playgroud)

另一个在res/values-large/dimen.xml中声明的字符串变量as

<string name="orientation">vertical</string>
Run Code Online (Sandbox Code Playgroud)

然后,当我在LinearLayout中设置方向时,我认为我可以使用

android:orientation="@string/orientation"
Run Code Online (Sandbox Code Playgroud)

但这没效果.我最终做的是拆分布局.我最初对两个单独的布局有所保留,因为我认为我会有一些重复的代码用于一个简单的更改.那是在我了解包含和合并之前.首先,我创建了一个公共布局文件,即res/layout/content.xml中的图像和文本,如下所示:

<merge xmlns:android="http://schemas.android.com/apk/res/android"  >

    <ImageView android:id="@+id/cover"
            android:layout_width="@dimen/cover_width"
            android:layout_height="@dimen/cover_height"/>

    <TextView android:id="@+id/title"
            android:gravity="top"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000000"
            android:textSize="@dimen/title_font_size"
            android:scrollHorizontally="true"
            android:maxLines="1"
            android:ellipsize="end" />
</merge>
Run Code Online (Sandbox Code Playgroud)

(旁注:我最初对合并标签的作用感到困惑.它没有合并合并标签内部的内容(在这个例子中是图像和文本)它基本上说什么父文件包含这个文件,合并内容在标签到父文件中)

然后我为LinearLayout创建了两个单独的文件,其中包含图像和描述xml文件.一个在res/layout/container.xml中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:id="@+id/library_item_container"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

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

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

和res/layout-large/container.xml中的一个:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:id="@+id/library_item_container"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

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

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

请注意,两个container.xml文件之间的唯一区别是方向已从垂直更改为水平.现在有最少的代码重复,问题解决了!

  • 如果您使用“ 0”代替字符串“ horizo​​ntal”,而用“ 1”代替“ vertical”,那么使用@ string / orientation的原始,简单方法就可以了。 (2认同)