- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个查询数据库并获取记录集的应用程序,在显示器上,我需要用复杂布局的行来呈现这些数据。每行包含一些 ImageView,许多 TextView 等...
以编程方式创建行布局真的很困难,有没有办法从 xml 中获取整个行布局(行布局的容器和子级),编辑一些属性(如行布局的 TextViews)并将结果添加到 LinearLayout?
最佳答案
is there any way to get the entire row layout (container and childs of the row layout) from an xml
您正在寻找的是如何inflate
View ( LayoutInflator )
现在您已经有了正确的术语,应该很容易找到示例,inflate
在 ListView
教程中很流行。例如,查看本教程中的 getView()
示例:
HowTo:ListView、Adapter、getView 和不同列表项的布局在一个 ListView 中
http://android.amberfog.com/?p=296
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
...
convertView = mInflater.inflate(R.layout.item1, null);
... edit some of the properties (like the TextViews of the row layout) ...
展开 View 后,您可以在其中搜索小部件,以便对其进行操作。
holder.textView = (TextView) convertView.findViewById(R.id.text);
如果您的 View 相当复杂和/或您经常在其中寻找小部件,我想指出 ViewHolder 技术,如引用的示例所示,相关位如下:
// Data structure to save lookups
public static class ViewHolder {
public TextView textView;
}
...
// Save lookups to widgets for this view in ViewHolder in tag
ViewHolder holder = new ViewHolder();
holder.textView = (TextView) convertView.findViewById(R.id.text);
view.setTag(holder);
...
// Grab saved widgets - no need to search tree for them via lookup again
ViewHolder holder = (ViewHolder) convertView.getTag();
holder.textView.setText(mData.get(position));
... and add the result to a LinearLayout?
据推测,您已经以编程方式添加到 LinearLayout
,但如果您想查看一些代码,这里有一个显示设置一些布局参数的示例:
安卓线性布局
http://developerlife.com/tutorials/?p=312
// main "enclosing" linearlayout container - mainPanel
final LinearLayout mainPanel = new LinearLayout(ctx);
{
mainPanel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
mainPanel.setOrientation(LinearLayout.VERTICAL);
...
}
...
// top panel
LinearLayout topPanel = new LinearLayout(ctx);
{
// WEIGHT = 1f, GRAVITY = center
topPanel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT,
1));
...
}
...
// bottom panel
LinearLayout bottomPanel = new LinearLayout(ctx);
{
LayoutUtils.Layout.WidthFill_HeightWrap.applyLinearLayoutParams(bottomPanel);
...
}
...
// add the panels
mainPanel.addView(topPanel);
mainPanel.addView(bottomPanel);
...
最后,您可以使用 AdapterView
/Adapter
范例做很多事情(包括自定义行),例如将 ListView
与 SimpleCursorAdapter
结合使用。通过查看它可能会为您节省一些代码。在这里胡说八道:
关于android - 为每条记录重复一个复杂的Layout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4793069/
我刚开始使用 Gnu Plot 并创建了一些简单的绘图。但是现在我遇到了一个新问题。 输入是这样的 csv 文件: name;n0;n1;n2 Benj;1;3;2 Silv;6;1;2 Steffi
我在 MongoDB 中有 2700 条记录。每个文档的大小约为 320KB。我使用的引擎是wiredTiger,集合的总大小约为885MB。 我的 MongoDB 配置如下: systemLog:
我是一名优秀的程序员,十分优秀!