9 android listview android-listview layout-inflater
public class MobileArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MobileArrayAdapter(Context context, String[] values) {
super(context, R.layout.list_mobile, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_mobile, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
textView.setText(values[position]);
Run Code Online (Sandbox Code Playgroud)
谢谢,
Joe*_*oel 21
什么是Inflater?
总结一下LayoutInflater文档所说的内容...... A LayoutInflater是Android系统服务之一,它负责获取定义布局的XML文件,并将它们转换为View对象.然后,操作系统使用这些视图对象来绘制屏幕.
我总是对我们为什么需要在android中使用inflater感到含糊不清,为什么它们在Android ListView中用于自定义布局?
通常,您不需要直接使用LayoutInflater.Android为大多数布局通胀的你,当你调用setContentView()在onCreate()您的活动的方法. 因此,作为程序员,您有责任确保视图膨胀.现在,您想要在ListView的上下文中扩展视图.如果您不想自定义每个项目,Adapter类可以为您进行通胀.但是,如果要自定义列表中显示的视图,则必须使用LayoutInflater手动为每个视图充气,因为您不能使用其他现有方法.
使用Inflater有什么好处?
使用它没有任何好处.您需要使用某种形状或形式的LayoutInflater来扩展静态XML布局.
或者,您可以使用Java代码动态创建视图.但是,您需要调用方法来手动设置视图的每个属性.在我看来,使用XML /通胀过程更容易.此外,Android会在构建时预处理您的XML文件,因此这会缩短执行时间.
| 归档时间: |
|
| 查看次数: |
27031 次 |
| 最近记录: |