出于测试目的:
这里,例如:
<TextView
android:id="@id/expander"
android:text="Fewer Details"
android:layout_gravity="center_vertical|start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
gravity int equivalent is:8388627。
如何重新分析8388627获取字符串&#34; center_vertical | start&#34; ?
答案 0 :(得分:1)
查看Gravity
对象,需要将XML描述符转换为对象的常量:
center_vertical
- &gt; Gravity.CENTER_VERTICAL
常数值:16(0x00000010)start
- &gt; Gravity.START
常数值:8388611(0x00800003)你添加它们就可以得到你的价值(8388627)。
反过来可以通过3个步骤进行逻辑运算:
Gravity.RELATIVE_LAYOUT_DIRECTION
)LEFT + RELATIVE_LAYOUT_DIRECTION = START
) - 这是可选的我的方法是:
Create a Map<int, String> modifiers
Create a Map<int, String> flags
Create a List<int> components
Populate modifiers with all generic flags and their corresponding strings
Use something like (flag > 0x0010000 && (flag & 0x0000FFFF == 0))
Populate flags with all other elements ( < 0x0010000)
// Note: Don't populate modifiers with elements like Gravity.START
int value = my_value_to_parse
for (int i in modifiers.keys()) {
if (value & i > 0) {
components.add(i)
value = value & !i
}
}
// Same code for the flags
for (int i in flags.keys())
...
// One can add logic for merging composite flags here
// And add the components to a string
String result = ""
for (int component: components) {
if (modifiers.containsKey(component))
result += modifiers.get(component)
else if (flags.containsKey(component))
result += flags.get(component)
result += "|"
// Return everything except last | character
return result.substring(0, result.length() - 2)
您可以使用this question生成Map
字符串和代码。
要将列表转换为字符串,您还可以创建一个List并使用Join
(guava)或Apache Commons。