- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在构建一个动态打印值/项目列表的配置程序。有时您需要输出一项,例如姓名,但有时您可能需要输出多项,例如 IP 地址、用户名、密码等。
这个想法是根据输出的参数数量动态地建立一个列表。在小屏幕上,参数的数量可能是 2,但在横向模式或平板电脑上,参数的数量可能要高得多。
当我有一个带有 Android ID 的硬编码 XML 文件时,我有这个工作,即引用
android:id="@+id/item1
但我正在努力以编程方式执行此操作。在编辑之前,我用 ID 来做这件事,但根据建议,我现在宁愿用标签来尝试。
这里是代码的所有相关部分。如果输出多行参数列表,我们分支到 buildLayout 来构建自定义布局:
public void processAsyncTask(String result) {
ConfigList list = getConfigList(result);
ArrayList<ConfigCollection> mParamsList = list.getAllItems();
// Once we have a list of parameters we build the layout according to size
buildLayout(mParamsList.size());
mMultiRowAdapter = new ParamsAdapter(this, R.layout.multi_row_config, mParamsList);
mCollectionListView.setAdapter(mMultiRowAdapter);
}
这是构建布局:
private void buildLayout(int size) {
TableRow tr = (TableRow)findViewById(R.id.row_multi_row);
for (int i = 0; i < size - 1; i++) {
TextView tv = new TextView(this);
tv.setId(i);
tv.setTag("id" + String.valueOf(i));
tv.setTextAppearance(this, android.R.style.TextAppearance_Large);
tv.setText("id" + String.valueOf(i));
tr.addView(tv);
}
}
下面是我尝试引用 ID 的方式:
/**
* Custom Array Adapter with a view that displays multi-line item/value pairs
*/
private class ParamsAdapter extends ArrayAdapter<ConfigCollection> {
private ArrayList<ConfigCollection> objectList;
public ParamsAdapter(Context context, int textViewResourceId, ArrayList<ConfigCollection> objectList) {
super(context, textViewResourceId, objectList);
this.objectList = objectList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.multi_row_config, null);
// It works with hard coded IDs, e.g.:
TextView tv1 = (TextView) v.findViewById(R.id.item1);
TextView tv2 = (TextView) v.findViewById(R.id.item2);
// BELOW DOES NOT, tv1 and tv2 is null after assignment
TextView tv1 = (TextView) v.findViewWithTag("id0");
TextView tv2 = (TextView) v.findViewWithTag("id1");
这是我用来测试的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
android:id="@+id/table_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TableRow
android:id="@+id/row_multi_row"
>
<TextView
android:id="@+id/item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="item1"
android:padding="3dip"
/>
<TextView
android:id="@+id/item2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="item2"
android:padding="3dip"
/>
</TableRow>
</TableLayout>
编辑:我已经通过屏幕截图进一步隔离了问题。似乎在 buildLayout 中我添加了 TextView ,但在自定义数组适配器中,layoutinflator 找不到这些字段。我还更新了代码以尝试使用标签而不是 ID。
从 buildLayout 调试。注意四个 child ,两个固定在 XML 代码中,另外两个创建
从 ParamsAdapter 调试。请注意,在我实际尝试访问这些对象的这个阶段只有两个 child 。
最佳答案
如果您以编程方式创建项目,我建议您使用“tag”参数。它不如 id 快,但是您不必为此担心速度 - 只要您有父 View ,您就可以“findViewWithTag” - 参见 http://developer.android.com/reference/android/view/View.html - 向下滚动到 findViewWithTag
关于自定义列表适配器中动态创建的 TextView 上的 Android findViewById 似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9352031/
我在activity_main.xml中有一个Textview设计,我想用它来在另一个textview下面显示一个textview。我从 stackoverlow 本身获取了这段代码,并做了一些更改以
伙计们,我尝试添加另一个 TextView ,但是当我尝试时,新的 TextView 已放置在前一个 TextView 上,如何使第二个 TextView 作为新的第二个 TextView 下降 这是
现在 textview 显示如果我将滚动到底部的语句。看起来 promis textview 变大了。有办法解决吗? 星号下的所有内容都是 promis textview 的一部分。它是一个带有多个换
我有多个 TextView 。所有 TextView 都是一个单词。我想用它们来构建一个段落。但他们不应该失去他们的 TextView 功能。因此它们不应该用作字符串。因为那样的话,我希望它们可以一一
附件是指向我的应用程序屏幕截图的链接和显示我想要实现的功能的视频的链接。基本上我想要的是当我单击任何功能区上的“更多”按钮时,它应该自行展开并且用从 JSON 文件中获取的数据替换文本。功能区的高度应
我想显示 edittext 中的文本,但由于输入了大量字符,布局被破坏了。我如何限制在 textview 上显示的字符数 最佳答案 你可以尝试在 Edittext actionlistener 上使用
是否可以将 textview 环绕在 textview 周围,第二个 textview 将环绕到第一个 textview 下的下一行? 例如: 我尝试过使用 android:layout_weig
假设我有 10 个文本项,所有这些项都对用户可见。每个文本项都有不同的颜色和样式。我不知道实现此目标的最佳方法是什么。 多个静态 TextView - 最容易实现,但性能可能最差。 TextView
我正在尝试创建一个布局,其中有两个 TextViews 彼此相邻。第一个 TextView 具有可变长度并且是单行的。第二个 TextView 包含一个固定标签,应该不被省略。第二个标签必须紧挨着第一
我正在使用 onTouchEvent 方法来缩放 TextView ,但是当我有两个 TextView 并且我只想调整第一个 TextView 而不是第二个 TextView 时,我遇到了麻烦。我的意
在我的 android 应用程序中,我有一个自动完成 TextView 字段,可以向用户建议不同的州名称。代码如下: ArrayAdapter state_adapter=new ArrayAdap
我怎样才能让 textview 开始一个新行,一直从左边开始,同时保留属性“toTheRightOf”username25? 像这样: 最佳答案 我的猜测是 twitch 应
我正在做一个动态 TextView 的例子,它是通过按下一个按钮创建的。它们的创建格式类似于: 1- Textview (fist created) 2- TextView 3- TextView (
我想要 TextView 和描述的自动调整大小的文本 here .我想,“测试”这个词应该在单行上对齐,但它是基于字符包装的(参见另一行的“g”)。我应该如何更改我的配置,以便一切都按我的意愿工作?
我有一个简单的布局,在两个 ImageView 之间包含一个 TextView。 当我在运行时使用“setText()”更改 TextView 时,它发生了变化,但失去了“center_horizo
作为 Android 初学者,我正在尝试显示一棵树。我已经找到了如何以编程方式添加新的 TextView,但我不知道如何放置它们,我的意思是如何根据父节点设置它们的填充/边距。 非常感谢您的回答, 巴
对于 AndroidTV,我创建了一个简单的搜索栏和 TextView 。我正在尝试对齐搜索栏拇指的 TextView 并尝试随着进度移动。 尝试了这些解决方案,但不起作用 link1 , link2
我是 Android Studio 新手,正在编写应用程序。 我正在尝试它,我想知道如何在 Textview1 包含某些内容的情况下启用(例如)Textview2。 我尝试了这段代码,但它不起作用:
我从这里获取 TextView 实例: TextView date = null; try { date = (TextView) getLayoutI
这是在别处被问到的,但解决方案对我不起作用。所以用更多的背景再次摆姿势。问题是一个 Activity 包含一个滚动的音乐标题 TextView ,它被更新的耗时计数器 TextView 中断。 我的
我是一名优秀的程序员,十分优秀!