您好亲爱的Stackoverflow社区。正如你猜测的那样,我来找你帮忙。
我有一个ListView,每行应遵循此布局row_layout.xml
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="@color/brownish">
<ImageView
android:id="@+id/passport_flag"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="6dp"
android:contentDescription="@string/country_flag" />
<TextView
android:id="@+id/exp_date"
android:layout_width="wrap_content"
android:layout_height="16dip"
android:ellipsize="marquee"
android:singleLine="true"
android:text="@string/exp_date"
android:textSize="12sp"
android:textColor="@color/white"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginStart="6dp" />
<TextView
android:id="@+id/birth_date"
android:layout_width="wrap_content"
android:layout_height="16dip"
android:ellipsize="marquee"
android:singleLine="true"
android:text="@string/birth_date"
android:textSize="12sp"
android:textColor="@color/white"
android:layout_above="@+id/exp_date"
android:layout_alignStart="@+id/exp_date" />
<TextView
android:id="@+id/document_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:layout_alignStart="@+id/exp_date"
android:layout_above="@id/birth_date"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:text="@string/document_number"
android:textSize="16sp"
android:textColor="@color/white" />
</RelativeLayout>
我实现了自定义ArrayAdapter
,其代码如下:
public class CustomArrayAdapter extends ArrayAdapter<EPassport> {
public static final String TAG = "CustomArrayAdapter";
private final String DOCUMENT_NUMBER = "Passport number: ";
private final String DATE_OF_BIRTH = "Date of birth: ";
private final String DATE_OF_EXPIRY = "Date of expiry: ";
private Context context;
private List<EPassport> ePassportsList;
private static class ViewHolder {
public TextView documentNumber;
public TextView birthDate;
public TextView expDate;
public ImageView flag;
}
public CustomArrayAdapter(Context context, List<EPassport> ePassportsList) {
super(context, R.layout.row_layout, ePassportsList);
this.context = context;
this.ePassportsList = ePassportsList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater =
(LayoutInflater) context
.getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.row_layout, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.documentNumber = (TextView) rowView.findViewById(R.id.document_number);
viewHolder.birthDate = (TextView) rowView.findViewById(R.id.birth_date);
viewHolder.expDate = (TextView) rowView.findViewById(R.id.exp_date);
viewHolder.flag = (ImageView) rowView.findViewById(R.id.passport_flag);
rowView.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
EPassport rowPassport = this.ePassportsList.get(position);
holder.expDate.setText(DATE_OF_EXPIRY + rowPassport.getDateOfExpiry());
holder.birthDate.setText(DATE_OF_BIRTH + rowPassport.getDateOfBirth());
holder.documentNumber.setText(DOCUMENT_NUMBER + rowPassport.getDocumentNumber());
if (rowPassport.getCountryCode() != null) {
String alpha2CountryCode = null;
try {
alpha2CountryCode = ISO3166Database.getAlpha2Code(rowPassport.getCountryCode());
} catch (IllegalCountryCodeException e) {
Log.e(TAG, e.getLocalizedMessage());
}
holder.flag.setImageResource(
getContext()
.getResources()
.getIdentifier(alpha2CountryCode, "drawable", getContext().getPackageName()));
}
return rowView;
}
}
我使用Android Studio 1.4和&#39; Design&#39;我row_layout.xml
的标签看起来就像我想要的那样。
但是,当我运行应用程序时,在三个文本字段中只有一个显示在屏幕上。
我无法弄清楚原因。如果有人愿意帮我一把,我将不胜感激。
由android studio呈现的行布局图像:
(点击图片放大)
屏幕上显示的图片:
答案 0 :(得分:1)
尝试将其用作行:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF664747"
android:padding="8dp">
<TextView
android:id="@+id/documentNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Document number: " />
<TextView
android:id="@+id/birthDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/documentNumber"
android:paddingTop="12dp"
android:text="Birth date: " />
<TextView
android:id="@+id/expireDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/birthDate"
android:text="Expire date: " />
<ImageView
android:id="@+id/passportFlag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/documentNumber"
android:src="@android:drawable/ic_dialog_email" />
</RelativeLayout>
只需更改数据和颜色。