如何在 android 中更改 ListView 中的项目?
How to change items in ListView in android?
我有一个主 activity,其中包含一个 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 and here and here 相矛盾,显示的四个条目非常小!我已经更改了属性 layout_height
(wrap_content、fill_parent、...)、layout_width
、minHeight
和 textSize
,但是 none 它似乎改变了每个 ListView
条目的整体大小。它们非常小,也不符合 AndroidStudio
中显示的预览。
如何更改 ListView
中项目的大小?
在 list_layout.xml 文件中,您可以增加 TextView 的文本大小。
在 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);
}
和构造函数中的LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
我有一个主 activity,其中包含一个 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 and here and here 相矛盾,显示的四个条目非常小!我已经更改了属性 layout_height
(wrap_content、fill_parent、...)、layout_width
、minHeight
和 textSize
,但是 none 它似乎改变了每个 ListView
条目的整体大小。它们非常小,也不符合 AndroidStudio
中显示的预览。
如何更改 ListView
中项目的大小?
在 list_layout.xml 文件中,您可以增加 TextView 的文本大小。
在 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);
}
和构造函数中的LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);