gpt4 book ai didi

android - 为每条记录重复一个复杂的Layout

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:42:15 25 4
gpt4 key购买 nike

我有一个查询数据库并获取记录集的应用程序,在显示器上,我需要用复杂布局的行来呈现这些数据。每行包含一些 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 )

现在您已经有了正确的术语,应该很容易找到示例,inflateListView 教程中很流行。例如,查看本教程中的 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 范例做很多事情(包括自定义行),例如将 ListViewSimpleCursorAdapter 结合使用。通过查看它可能会为您节省一些代码。在这里胡说八道:

Android ListView with different layouts for each row

关于android - 为每条记录重复一个复杂的Layout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4793069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com