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

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

概述我有一个简单的线性布局,我想根据设备的屏幕尺寸进行更改.我想要做的就像是 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="@string/cover_orientation" android:layout_width="fill_paren 我有一个简单的线性布局,我想根据设备的屏幕尺寸进行更改.我想要做的就像是
<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);}

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

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

谢谢!

解决方法 我最终通过浏览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中声明一个字符串变量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://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文件之间的唯一区别是方向已从垂直更改为水平.现在有最少的代码重复,问题解决了!

总结

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

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

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1134905.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存