我必须在我的Android应用程序中解析一些复杂的xml文件.像TouchXMl for iPhone那样有没有好的库?
我知道如何为视图创建自定义属性,例如:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SomeViewsCustomAttrs">
<attr name="someAttr" format="dimension" />
<attr name="anotherAttr" format="boolean" />
</declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)
我想知道是否有办法向这些自定义attrs添加文档.在XML编辑器中编辑布局时,您可以获得描述attrs内容的工具提示.例如,如果您键入android:layout_width=它,它将为您提供以下信息,"指定视图的基本宽度.[dimension,enum]"
有没有办法为你自己的attrs提供?
谢谢
所以我环顾四周,发现它android.R.styleable不再是SDK的一部分,即使它仍然在这里记录.
如果清楚地记录了备选方案的内容,那就不会成为问题.例如,AOSP日历应用程序仍在使用android.R.styleable
// Get the dim amount from the theme
TypedArray a = obtainStyledAttributes(com.android.internal.R.styleable.Theme);
lp.dimAmount = a.getFloat(android.R.styleable.Theme_backgroundDimAmount, 0.5f);
a.recycle();
Run Code Online (Sandbox Code Playgroud)
那么如果backgroundDimAmount没有得到它int[],怎么会得到android.R.styleable.Theme?
obtainStyledAttributes(int [])为了使其与SDK一起使用,我需要注意什么?
我有一个包含2个视图的FrameLayout,第二个是类似a的东西Close Button (X),我想把它放在右边.
我已经尝试过layout_gravity ="正确",但这不起作用.
这是我的布局xml:
<FrameLayout android:layout_width="320dp"
android:layout_height="wrap_content"
android:id="@+id/layoutSmallImg">
<ImageView android:id="@+id/bigImage"
android:layout_width="320dp" android:layout_height="wrap_content"/>
<ImageView android:id="@+id/bigImage"
android:layout_width="320dp" android:layout_height="wrap_content"/>
<ImageButton android:id="@+id/closebutton"
android:background="@android:color/transparent"
android:layout_height="30dp" android:layout_marginTop="10dp"
android:layout_alignParentRight="true"
android:layout_width="30dp" android:paddingLeft="40dp"
android:visibility="gone"
android:src="@drawable/close" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud) 我正在以编程方式创建一个自定义视图,显示从XML文件解析的文本.文本很长,包含强制换行符的"/ n"字符.由于某种原因,文本视图显示/ n并且没有任何换行符.这是我的代码:
// get the first section body
Object body1 = tempDict.get("FIRE");
String fireText = body1.toString();
// create the section body
TextView fireBody = new TextView(getActivity());
fireBody.setTextColor(getResources().getColor(R.color.black));
fireBody.setText(fireText);
fireBody.setTextSize(14);
fireBody.setSingleLine(false);
fireBody.setMaxLines(20);
fireBody.setBackgroundColor(getResources().getColor(R.color.white));
// set the margins and add to view
layoutParams.setMargins(10, 0, 10, 0);
childView.addView(fireBody,layoutParams);
Run Code Online (Sandbox Code Playgroud)
XML文件中的文本是这样的:
Now is the time /n for all good men to /n come to the aid of their /n party
Run Code Online (Sandbox Code Playgroud)
它应该如此显示;
Now is the time
for all good men to
come to the aid …Run Code Online (Sandbox Code Playgroud) 要创建自定义ToggleButton,我在以下位置定义了一个新样式/res/values/styles.xml:
<style name="myToggleButton">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#000000</item>
<item name="android:background">@drawable/my_toggle_button</item>
</style>
Run Code Online (Sandbox Code Playgroud)
然后我使用选择器指定按钮状态的外观/res/drawable/my_toggle_button.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<shape>
[...]
</shape>
</item>
<item android:state_checked="false"
<shape>
[...]
</shape>
</item>
</selector>
Run Code Online (Sandbox Code Playgroud)
当状态发生变化时,如何修改此设置以切换按钮的文本颜色?
如标题中所述,每次单击XML文件(布局或清单)时,进度显示"正在加载数据..." - 它会针对每个版本的API 2.1到4.4.2执行此操作.
有没有办法阻止这种行为.因为它减慢了系统的速度?
更新:我为此提出了一份AOSP 错误报告.
更新2:我很高兴地说AOSP团队现在已经提高了对"严重"修复的优先级.
更新3: 8天后,我不会高兴地说,尽管它被标记为一个关键的错误,它仍然在释放22.6.2等提供完整的修复.
更新4: 最后22.6.2发布,似乎工作正常.谢谢你们的作者.
假设我在android资源中存储了一个二维数组,如下所示.如何在像Arraylist这样的java集合中获取它们?
<resources>
<string-array name="countries_array">
<item>
<name>Bahrain</name>
<code>12345</code>
</item>
<item>
<name>Bangladesh</name>
<code>54545</code>
</item>
<item>
<name>India</name>
<code>54455</code>
</item>
</string-array>
</resources>
Run Code Online (Sandbox Code Playgroud)
例如,在一维数组的情况下,我们可以使用它
getResources().getStringArray(R.array.countries_array);
Run Code Online (Sandbox Code Playgroud)
当countries_array就像
<resources>
<string-array name="countries_array">
<item>Bahrain</item>
<item>Bangladesh</item>
<item>India</item>
</string-array>
</resources>
Run Code Online (Sandbox Code Playgroud) 我想实现水平进度条,其步骤如下面的img所示.

我在Android中找不到这样的原生组件.任何人都可以指导我如何做到这一点?
我正在尝试使用XML在Android应用中绘制对角线,但它无效.它只是绘制一条水平线.
main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".TestActivity" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
style="@style/diagonalStyle">
</RelativeLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
styles.xml:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="diagonalStyle">
<item name="android:background">@drawable/background</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
background.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<rotate
android:fromDegrees="0"
android:toDegrees="45"
android:pivotX="50%"
android:pivotY="50%" >
<shape
android:shape="line"
android:top="1dip" >
<stroke
android:width="1dip"
android:color="#FF0000" />
</shape>
</rotate>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud) android android-manifest android-layout android-xml android-drawable