我刚刚创建了我的第一个自定义适配器,效果很好。然后,我创建了一个 rowlayout.xml 供适配器填充。
但是我有一个问题,该行不是动态的,所以如果我尝试放入比该行长的文本,该行不会扩展,而是将文本切断。(见图片:http: //tinyurl.com/paxdt3a)
这是我的行布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<!--The second line is used for the date of the post-->
<TextView
android:id="@+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
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_width="fill_parent"
android:layout_height="wrap_content"
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_width="fill_parent"
android:layout_height="wrap_content"
android:padding="6dip" >
<!--The first line is used to the post-->
<TextView
android:id="@+id/firstLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
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_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:ellipsize="marquee"
android:singleLine="false"
android:text="Time and date"
android:textSize="12sp" />
</RelativeLayout>