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

时间:2011-06-22 19:16:56

标签: 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"/>

我在values和values-xlarge下创建了不同的dimen.xml文件,以便cover_orientation变量根据屏幕大小显示不同的值(“vertical”或“horizo​​ntal”)。

string name="cover_orientation">"vertical"

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

if(getResources().getString(R.string.screen_size) == "xlarge"){
    ((LinearLayout)convertView).setOrientation(1);
}

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

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

谢谢!

4 个答案:

答案 0 :(得分:10)

一个很好的方法是添加

android:orientation="@integer/cover_orientation"

LinearLayout上,并在下面定义。

values/consts.xml中的

<resources>

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

</resources>
values/something.xml中的

<resources>

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

</resources>
values-land/something.xml中的

<resources>

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

</resources>

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

答案 1 :(得分: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>

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

<string name="orientation">horizontal</string>

和res / values-large / dimen.xml中声明为

的另一个字符串变量
<string name="orientation">vertical</string>

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

android:orientation="@string/orientation"

但是这没用。我最终做的是拆分布局。我最初对两个单独的布局有所保留,因为我认为我会有一些重复的代码用于一个简单的更改。那是在我了解包含和合并之前。首先,我创建了一个公共布局文件,即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>

(旁注:我最初对合并标签做了什么感到困惑。它没有合并合并标签内部的内容(在这个例子中是图像和文本)它基本上是说无论父文件包含这个文件,合并标签之间的内容进入父文件)

然后我为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>

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

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

答案 2 :(得分:2)

@odiggity,感谢您发布此问题。我也在尝试。应用程序在启动时崩溃。

我认为这是一个运行时输入问题。换句话说,orientation属性只有两个合法值,不会被字符串“type”反映出来。框架可能需要做的是引入另一种专门的资源类型,类似于dimen或boolean。

我觉得你的问题有一个答案,它解决了比你上面的答案更多的案例。可以使用样式继承来定义除方向无关父样式中的方向之外的所有属性,然后仅在与方向相关的小样式定义中添加方向,并将其作为父级。

这样,即使在复杂的情况下也可以避免重复。

答案 3 :(得分:1)