我具有包括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;
}
}
简单地显示一个图标和一些文本。然而,显示的四个条目非常小,与我发现的例子here和here和here相反!我已更改属性layout_height
(wrap_content,fill_parent,...),layout_width
,minHeight
和textSize
,但似乎没有任何更改ListView
条目中的每一个的整体大小。他们保持非常小,也不符合AndroidStudio
中显示的预览。
如何更改ListView
中物品的尺寸?
嘿@Alex,请张贴** ** list_layout XML文件的内容
LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
。 –Ohhhh,我明白了。这些设置在OTHER布局中定义!我忘了。我想这已经是解决方案了。谢谢!! – Alex
不客气! –