2015-12-02 42 views
0

我具有包括ListView如下主要活動:如何在Android中更改ListView中的項目?

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context=".MainActivity" > 

    <android.support.design.widget.AppBarLayout 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary"/> 

    </android.support.design.widget.AppBarLayout> 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/main_grid" 

     android:useDefaultMargins="true" 
     android:alignmentMode="alignBounds" 
     android:columnOrderPreserved="false" 

     android:columnCount="4" 
     android:orientation="vertical"> 

     <TextView 
      android:text="MainTitle" 
      android:textSize="32dip" 
      android:layout_columnSpan="4" 
      android:gravity="center_horizontal" 
      android:id="@+id/textView1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

     <ListView 
      android:id="@+id/list" 
      android:layout_height="fill_parent" 
      android:layout_width="fill_parent" 
      android:padding="10dp" 
      android:minHeight="40dp" 
      android:textSize="30sp" 
      > 
     </ListView> 
    </LinearLayout> 

</android.support.design.widget.CoordinatorLayout> 

其中使用以下適配器(ListAdapter.java):

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class ListAdapter extends ArrayAdapter<String> { 
    private final Context context; 
    private final String[] values; 

    public ListAdapter(Context context, String[] values) { 
     super(context, R.layout.list_layout, values); 
     this.context = context; 
     this.values = values; 
    } 

    @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.list_layout, parent, false); 
     TextView textView = (TextView) rowView.findViewById(R.id.label); 
     ImageView imageView = (ImageView) rowView.findViewById(R.id.logo); 
     textView.setText(values[position]); 

     // Change icon based on name 
     String s = values[position]; 

     System.out.println(s); 

     if (s.equals("WindowsMobile")) { 
      imageView.setImageResource(R.drawable.icon4); 
     } else if (s.equals("iOS")) { 
      imageView.setImageResource(R.drawable.icon2); 
     } else if (s.equals("Blackberry")) { 
      imageView.setImageResource(R.drawable.icon3); 
     } else { 
      imageView.setImageResource(R.drawable.icon4); 
     } 

     return rowView; 
    } 
} 

簡單地顯示一個圖標和一些文本。然而,顯示的四個條目非常小,與我發現的例子hereherehere相反!我已更改屬性layout_height(wrap_content,fill_parent,...),layout_width,minHeighttextSize,但似乎沒有任何更改ListView條目中的每一個的整體大小。他們保持非常小,也不符合AndroidStudio中顯示的預覽。

如何更改ListView中物品的尺寸?

+2

嘿@Alex,請張貼** ** list_layout XML文件的內容LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE);。 –

+0

Ohhhh,我明白了。這些設置在OTHER佈局中定義!我忘了。我想這已經是解決方案了。謝謝!! – Alex

+0

不客氣! –

回答

2

在list_layout.xml文件中,您可以爲TextView增加textSize。

0

在方法getView中,您可以爲定義每個項目的佈局充氣。你應該改變物品的屬性。 另外,您應該爲第一個項目膨脹此佈局。你的代碼應該是

public View getView(int position, View convertView, ViewGroup parent) { 

    if(convertView == null || !convertView.getTag().equals(TAG_NULL)) { 
     convertView = inflater.inflate(R.layout.list_layou, parent, false); 
    } 

,並在構造函數中

相關問題