Android - 如何根据设备在 xml 中设置可见性?
Android - How to set visibility in xml depending on device?
我的 activity 中有一个 TextView
,通常应该是 visible
,但对于平板设备来说应该是 gone
。
我知道我可以为平板电脑创建一个新的布局文件,但这似乎有很多重复,所以我想做的是在我的(单个)布局文件中设置类似...
android:visibility="@string/my_textview_visibility"
...对于 TextView
。然后,在我的字符串资源文件中,设置...
<string name="my_textview_visibility">gone</string>
(在values/strings.xml)
...和...
<string name="my_textview_visibility">visible</string>
(在values-sw720dp/strings.xml)
...隐藏平板电脑的 TextView。
但是,当我尝试此操作时,应用程序在尝试显示 activity 时崩溃了。
我是否需要使用常量值而不是上述字符串值 - 例如,
"visible"
-> 0
"gone"
-> 8
..如果是这样,add/reference 这些值 to/from 我的 XML 文件的正确方法是什么?
或者有another/better的方法吗?
注意 - 我不想在我的 Java 代码中以编程方式 show/hide TextView
。
您应该改用 /values/integers/
:
values/integers.xml
<integer name="my_textview_visibility">0</integer> <!-- 0 = View.VISIBLE -->
values-sw720dp/integers.xml
<integer name="my_textview_visibility">2</integer> <!-- 2 = View.GONE -->
然后这样调用:
android:visibility="@integer/my_textview_visibility"
如您所说,最好的方法是为 tablets
创建一个特定的 layout
,您的 TextView
将被隐藏。但是,可以使用 xml boolean resources
来做到这一点:(如 res/values-sw600dp/
):
<resources>
<bool name="isTablet">true</bool>
</resources>
因为sw600dp
限定符只对android3.2以上的平台有效。如果您想确保此技术适用于所有平台(3.2 之前),请在 res/values-xlarge
文件夹中创建相同的文件:
<resources>
<bool name="isTablet">true</bool>
</resources>
然后,在 "standard" 值文件中(如 res/values/
),将布尔值设置为 false:
<resources>
<bool name="isTablet">false</bool>
</resources>
然后在你activity,你可以得到这个值并检查你是否运行在一个平板大小的设备中:
boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
if (tabletSize) {
// do something
} else {
// do something else
}
字体:Determine if the device is a smartphone or tablet?
ChrisStillwell 的回答是正确的。但根据 Android 文档
,这些值不正确
我的 activity 中有一个 TextView
,通常应该是 visible
,但对于平板设备来说应该是 gone
。
我知道我可以为平板电脑创建一个新的布局文件,但这似乎有很多重复,所以我想做的是在我的(单个)布局文件中设置类似...
android:visibility="@string/my_textview_visibility"
...对于 TextView
。然后,在我的字符串资源文件中,设置...
<string name="my_textview_visibility">gone</string>
(在values/strings.xml)
...和...
<string name="my_textview_visibility">visible</string>
(在values-sw720dp/strings.xml)
...隐藏平板电脑的 TextView。
但是,当我尝试此操作时,应用程序在尝试显示 activity 时崩溃了。
我是否需要使用常量值而不是上述字符串值 - 例如,
"visible"
-> 0
"gone"
-> 8
..如果是这样,add/reference 这些值 to/from 我的 XML 文件的正确方法是什么?
或者有another/better的方法吗?
注意 - 我不想在我的 Java 代码中以编程方式 show/hide TextView
。
您应该改用 /values/integers/
:
values/integers.xml
<integer name="my_textview_visibility">0</integer> <!-- 0 = View.VISIBLE -->
values-sw720dp/integers.xml
<integer name="my_textview_visibility">2</integer> <!-- 2 = View.GONE -->
然后这样调用:
android:visibility="@integer/my_textview_visibility"
如您所说,最好的方法是为 tablets
创建一个特定的 layout
,您的 TextView
将被隐藏。但是,可以使用 xml boolean resources
来做到这一点:(如 res/values-sw600dp/
):
<resources>
<bool name="isTablet">true</bool>
</resources>
因为sw600dp
限定符只对android3.2以上的平台有效。如果您想确保此技术适用于所有平台(3.2 之前),请在 res/values-xlarge
文件夹中创建相同的文件:
<resources>
<bool name="isTablet">true</bool>
</resources>
然后,在 "standard" 值文件中(如 res/values/
),将布尔值设置为 false:
<resources>
<bool name="isTablet">false</bool>
</resources>
然后在你activity,你可以得到这个值并检查你是否运行在一个平板大小的设备中:
boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
if (tabletSize) {
// do something
} else {
// do something else
}
字体:Determine if the device is a smartphone or tablet?
ChrisStillwell 的回答是正确的。但根据 Android 文档
,这些值不正确