控制从资源来看知名度 [英] Controlling view visibility from a resources
问题描述
我有一个包含两个ImageViews的布局。我想其中的一个可见的肖像和其他景观。我如何实现它使用的资源? (我知道如何编程设置它,但这个特定的使用,我需要用资源来实现它)。
I have a layout that contains two ImageViews. I want one of them to be visible in portrait and the other in landscape. How do I achieve it using resources? (I know how to set it programmatically for but this specific use I need to achieve it using resources).
我想是这样
在RES /布局/ may_layout.xml:
in res/layout/may_layout.xml:
...
<ImageView
android:id="@+id/image1"
android:visibility="@integer/visible_in_portrait" <<-- is this allowed?
...
/>
<ImageView
android:id="@+id/image2"
android:visibility="@integer/visible_in_landscape"
...
/>
在RES /价值/ integers.xml:
in res/values/integers.xml:
...
<!-- NOTE: 0 and 8 are VISIBLE and GONE respectively -->
<integer name="visibile_in_portrait">0</integer>
<integer name="visibile_in_landscape">8</integer>
在RES /值陆/ integers.xml:
in res/values-land/integers.xml:
...
<integer name="visibile_in_portrait">8</integer>
<integer name="visibile_in_landscape">0</integer>
但我得到一个运行时错误(索引出界)试图夸大的图像时。当我删除了android:可见性语句,程序运行,但我看到这两个图片
But I get a runtime error (index out of bound) when trying to inflate the images. When I remove the android:visibility statements, the program runs but I see both images.
问:什么是使用资源作为Android的一个值的方式:?可见性属性
Q: What is the way to use a resource as a value for the android:visibility attribute?
(如果你想知道为什么编程方式设置它不会帮我,它必须具有自动解构/应用程序部件的使用文件URI位图画像开关)。
(if you wonder why setting it programmatically will not help me, it has to do with automatic landspace/portrait switch of app widgets with file uri bitmaps).
推荐答案
这将是可能的,当你使用这一招,你的知名度行添加到一个样式,并把该文件的两个实例中-land和正常模式。
it will be possible when you use this trick, add your visibility line to a style and put two instances of that file in -land and normal mode.
我的意思是在文件夹中的文件 styles.xml
值
把风格的名称 S1
,并把安卓知名度=可见
在于,在 styles.xml
在文件夹值陆
把风格的名称 S1
,并把安卓知名度=走了
。
I mean in file styles.xml
in folder values
put a style with name s1
, and put android:visibility=visible
in that, and in styles.xml
in folder values-land
put a style with name s1
, and put android:visibility=gone
.
此外,在文件 styles.xml
文件夹值
把风格的名称 S2
,并把安卓知名度=不见了
在于,在 styles.xml
在文件夹值陆
把风格的名称 S2
,并把安卓知名度=可见
。
also, in file styles.xml
in folder values
put a style with name s2
, and put android:visibility=gone
in that, and in styles.xml
in folder values-land
put a style with name s2
, and put android:visibility=visible
.
然后设置 S1
来第一的ImageView和 S2
第二。
and then, set s1
to first imageview and s2
to second.
由亲爱的卡文给出的解决方案也是正确的,但是当你有一个复杂的布局,可能在时间发生变化,有一个布局文件会更好,而且将有较少需要改变。
solution given by dear Calvin is also correct, but when you have a complex layout that may change during time, having one layout file would be better, and will have less need to change.
这篇关于控制从资源来看知名度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!