0
我跟隨this教程,瞭解如何向列表視圖中的單個列表元素添加標題,文本視圖id/txtTitle
和圖像視圖id/imgIcon
。如何添加第三個文本視圖以使用自定義列表適配器列出視圖元素
我修改了教程代碼以合併第三個文本視圖id/txtTitle2
,但是我仍然只能看到圖像視圖id/imgIcon
和原始文本視圖id/txtTitle
。此外,列表視圖好像它是更大的,或者放大,這是我的代碼修改的結果。
如何解決的代碼,所以我看到沒有大小失真的第三個文本看法?
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
tools:context=".MainActivity">
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
listview_item_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
<ImageView android:id="@+id/imgIcon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
<TextView android:id="@+id/txtTitle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:textStyle="bold"
android:textSize="22dp"
android:textColor="#000000"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
<TextView android:id="@+id/txtTitle2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:textStyle="bold"
android:textSize="22dp"
android:textColor="#000000"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
</LinearLayout>
自定義列表適配器WeatherAdapter extends ArrayAdapter<Weather>
public class WeatherAdapter extends ArrayAdapter<Weather>{
Context context;
int layoutResourceId;
Weather data[] = null;
public WeatherAdapter(Context context, int layoutResourceId, Weather[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
WeatherHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new WeatherHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
holder.txtTitle2 = (TextView)row.findViewById(R.id.txtTitle2);
row.setTag(holder);
}
else
{
holder = (WeatherHolder)row.getTag();
}
Weather weather = data[position];
holder.txtTitle2.setText(weather.title2);
holder.txtTitle.setText(weather.title);
holder.imgIcon.setImageResource(weather.icon);
return row;
}
static class WeatherHolder
{
ImageView imgIcon;
TextView txtTitle;
TextView txtTitle2;
}
}
類Weather
public class Weather {
public int icon;
public String title;
public String title2;
public Weather(){
super();
}
public Weather(int icon, String title, String title2) {
super();
this.icon = icon;
this.title = title;
this.title2 = title2;
}
}
謝謝。有一個問題的暗示。任何想法爲什麼我不能給ImageView添加權重?重量只能用於TextViews。 –
我認爲這個清單是我自己的。謝謝。 –
您也可以爲ImageView添加權重。它的工作原理與TextView完全相同。在處理權重之前,您需要理解幾件事情。如果您分配權重只是一個視圖兩枚,它會佔用所有可用的第二個觀點已採取了後面的空格,它需要的空間。此外,它最好設置時權重分配給它0dp視圖的寬度/高度。 – Rachit