Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android:如何使用xml字符串值来确定布局方向大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个简单的线性布局,我想根据设备的屏幕尺寸进行更改.我想要做的就像是
<LinearLayout xmlns:android="http://scheR_477_11845@as.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);
}

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

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

谢谢!

解决方法

我最终通过浏览android文档找到了解决问题的方法.我最初拥有的是一个LinearLayout,其中包含一个图像内部的文本(其中实际上有更多的内容,但我会在这个例子中保持简单),看起来像这样
<LinearLayout xmlns:android="http://scheR_477_11845@as.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中声明一个字符串变量as

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

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

<String name="orientation">vertical</String>

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

android:orientation="@String/orientation"

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

<merge xmlns:android="http://scheR_477_11845@as.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://scheR_477_11845@as.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://scheR_477_11845@as.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文件间的唯一区别是方向已从垂直更改为水平.现在有最少的代码重复,问题解决了!

大佬总结

以上是大佬教程为你收集整理的Android:如何使用xml字符串值来确定布局方向全部内容,希望文章能够帮你解决Android:如何使用xml字符串值来确定布局方向所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。