Mar*_*ion 4 xml layout android
我有一个查询数据库并获取记录集的应用程序,在显示器上,我需要使用一个复杂布局的行来呈现这些数据.每行包含一些ImageView,许多TextView等...
以编程方式创建行布局真的很困难,有没有办法从xml中获取整个行布局(行布局的容器和子行),编辑一些属性(如行布局的TextViews)并添加结果为LinearLayout?
Ber*_*t F 10
有没有办法从xml获取整个行布局(行布局的容器和子项)
您正在寻找的是如何inflate视图(LayoutInflator)
现在你有了正确的术语,它应该很容易找到示例,inflate在ListView教程中很受欢迎.有关示例,请参阅getView()本教程中的示例:
HowTo:ListView,适配器,getView和不同列表项的布局在一个ListView
http://android.amberfog.com/?p=296
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
...
convertView = mInflater.inflate(R.layout.item1, null);
Run Code Online (Sandbox Code Playgroud)
...编辑一些属性(比如行布局的TextViews)......
一旦您为视图充气,您就可以搜索其中的小部件,以便您可以对其进行操作.
holder.textView =(TextView)convertView.findViewById(R.id.text);
如果您的视图相当复杂和/或您经常在其中查找小部件,我想指出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));
Run Code Online (Sandbox Code Playgroud)
...并将结果添加到LinearLayout?
据推测,您已经以编程方式添加了LinearLayout,但如果您想查看一些代码,这里有一个示例,显示了设置一些布局参数:
Android 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);
...
Run Code Online (Sandbox Code Playgroud)
最后,您可以使用AdapterView/ Adapterparadigm 进行很多(包括自定义行),例如使用ListViewa SimpleCursorAdapter.它可以通过调查来节省一些代码.有些人在这里喋喋不休:
| 归档时间: |
|
| 查看次数: |
5925 次 |
| 最近记录: |