android自定义适配器动态行高
Posted
技术标签:
【中文标题】android自定义适配器动态行高【英文标题】:android custom adapter dynamic row height 【发布时间】:2014-10-15 09:56:14 【问题描述】:我刚刚创建了我的第一个自定义适配器,效果很好。 然后,我创建了一个 rowlayout.xml 供适配器填充。
但我有一个问题,该行不是动态的,所以如果我尝试放入比该行长的文本,该行不会扩展,而是将文本截断。 (见图片:http://tinyurl.com/paxdt3a)
这是我的行布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:padding="6dip" >
<!--The second line is used for the date of the post-->
<TextView
android:id="@+id/secondLine"
android:layout_
android:layout_
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Time and date"
android:textSize="12sp" />
<!--The first line is used to the post-->
<TextView
android:id="@+id/firstLine"
android:layout_
android:layout_
android:layout_above="@id/secondLine"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:gravity="center_vertical"
android:text="Post"
android:textSize="16sp" />
</RelativeLayout>
这是我的适配器:
public class MyAdapter extends ArrayAdapter<UserPost>
private final Context context;
private final List<UserPost> posts;
public MyAdapter(Context context, List<UserPost> posts)
super(context, R.layout.rowlayout, posts);
this.context = context;
this.posts = posts;
@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.rowlayout, parent, false);
TextView firstText = (TextView) rowView.findViewById(R.id.firstLine);
TextView secondText = (TextView) rowView.findViewById(R.id.secondLine);
//Insert the post
firstText.setText(posts.get(position).getMessage());
//Insert the time
secondText.setText(posts.get(position).getDate());
return rowView;
有人知道如何解决这个问题吗?
谢谢!
更新(解决方案):
好的,所以我在 rowlayout.xml 中进行了一些更改,我终于想出了这个,它有效!
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:padding="6dip" >
<!--The first line is used to the post-->
<TextView
android:id="@+id/firstLine"
android:layout_
android:layout_
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:gravity="center_vertical"
android:text="Post"
android:textSize="16sp" />
<!--The second line is used for the date of the post-->
<TextView
android:id="@+id/secondLine"
android:layout_below="@+id/firstLine"
android:layout_
android:layout_
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:ellipsize="marquee"
android:singleLine="false"
android:text="Time and date"
android:textSize="12sp" />
</RelativeLayout>
【问题讨论】:
【参考方案1】:在您的item layout
上,您有android:layout_height="?android:attr/listPreferredItemHeight"
,这意味着您强制将所有行的特定行高更改为android:layout_height="wrap_content"
【讨论】:
【参考方案2】:你可以做的就是删除 singleLine 属性
android:singleLine="false"
或者你可以这样做
public class MyAdapter extends ArrayAdapter<UserPost>
private final Context context;
private final List<UserPost> posts;
public MyAdapter(Context context, List<UserPost> posts)
super(context, R.layout.rowlayout, posts);
this.context = context;
this.posts = posts;
@Override
public View getView(int position, View convertView, ViewGroup parent)
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(text.length>MAX_LENGTH)
View rowView = inflater.inflate(R.layout.rowlayout1, parent, false);
//....
else
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
TextView firstText = (TextView) rowView.findViewById(R.id.firstLine);
TextView secondText = (TextView) rowView.findViewById(R.id.secondLine);
//Insert the post
firstText.setText(posts.get(position).getMessage());
//Insert the time
secondText.setText(posts.get(position).getDate());
return rowView;
【讨论】:
以上是关于android自定义适配器动态行高的主要内容,如果未能解决你的问题,请参考以下文章
使用适用于 Android 应用的自定义适配器将项目动态添加到列表视图
POI导出excel:设置字体颜色行高自适应列宽自适应锁住单元格合并单元格...