Android 中使用 XML 布局的自定义视图

发布于 2024-10-29 08:37:37 字数 185 浏览 2 评论 0原文

我有一个 ListAdapter,其中有很多不同的行布局。为了获得干净的代码,我想将视图类中适配器的 getView() 的行布局外包。是否可以将 XML 布局扩展为自定义视图?我只找到了 LayoutInflater,但它返回一个 View,这没有帮助。我想要类似 Activity 的 setLayout() 的东西。这可能吗?

谢谢!

I have a ListAdapter with a lot of different layouts for the rows. To have a clean code I want to outsource the layouts for the rows from the getView() of the adapter in View classes. Is it possible to inflate a XML layout into a custom view? I've only found the LayoutInflater but it returns a View and that does not help. I want to have something like the setLayout() of an Activity. Is this possible?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怀中猫帐中妖 2024-11-05 08:37:37

您可以拥有自定义行视图并在其构造函数中膨胀您的 xml:

public MyRow extends LinearLayout {
    public MyRow(Context context) {
        super(context);
        LayoutInflater.from(context).inflate(R.layout.my_row, this, true);
          ... other initialization ...
    }
}

然后在 my_row.xml 中使用 merge

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
  ... your row layout ...
</merge>

merge 元素会导致其要添加为自定义视图的子级的子级。查看合并布局了解更多信息。

You can have a custom row view and inflate your xml in its constructor:

public MyRow extends LinearLayout {
    public MyRow(Context context) {
        super(context);
        LayoutInflater.from(context).inflate(R.layout.my_row, this, true);
          ... other initialization ...
    }
}

and then use merge in my_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
  ... your row layout ...
</merge>

The merge element causes its children to be added as children of your custom view. Check out Merging Layouts for more info.

那一片橙海, 2024-11-05 08:37:37

我一直使用带有视图的自定义适配器,例如:

public class CalendarAdapter extends BaseAdapter {
protected static CalViewHolder holder;
private LayoutInflater mInflater;
public HashMap<Integer,String[]> appointments = new HashMap<Integer,String[]>();

public CalendarAdapter(Context context,HashMap<Integer,String[]> set_appointments) {
    // Cache the LayoutInf
     mInflater = LayoutInflater.from(context);
     appointments = set_appointments;
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return appointments == null ? 0:appointments.size();
}
@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}
@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
     if (convertView == null) {
         convertView = mInflater.inflate(R.xml.appointment, null);
         holder = new CalViewHolder();
         holder.app_lay = (LinearLayout) convertView.findViewById(R.id.appointment_layout);
         holder.app_head = (TextView) convertView.findViewById(R.id.appointment_head);
         holder.app_body = (TextView) convertView.findViewById(R.id.appointment_body);
         convertView.setTag(holder);
     }else{
        holder = (CalViewHolder) convertView.getTag();
     }
     holder.app_head.setText(appointments.get(position)[0]);
     holder.app_body.setText(appointments.get(position)[1]);
     return convertView;
}

static class CalViewHolder {
    LinearLayout app_lay;
    TextView app_head;
    TextView app_body; 
}

}

I alwas use a custom Adapter with a viewholder like:

public class CalendarAdapter extends BaseAdapter {
protected static CalViewHolder holder;
private LayoutInflater mInflater;
public HashMap<Integer,String[]> appointments = new HashMap<Integer,String[]>();

public CalendarAdapter(Context context,HashMap<Integer,String[]> set_appointments) {
    // Cache the LayoutInf
     mInflater = LayoutInflater.from(context);
     appointments = set_appointments;
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return appointments == null ? 0:appointments.size();
}
@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}
@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
     if (convertView == null) {
         convertView = mInflater.inflate(R.xml.appointment, null);
         holder = new CalViewHolder();
         holder.app_lay = (LinearLayout) convertView.findViewById(R.id.appointment_layout);
         holder.app_head = (TextView) convertView.findViewById(R.id.appointment_head);
         holder.app_body = (TextView) convertView.findViewById(R.id.appointment_body);
         convertView.setTag(holder);
     }else{
        holder = (CalViewHolder) convertView.getTag();
     }
     holder.app_head.setText(appointments.get(position)[0]);
     holder.app_body.setText(appointments.get(position)[1]);
     return convertView;
}

static class CalViewHolder {
    LinearLayout app_lay;
    TextView app_head;
    TextView app_body; 
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文