我正在使用ListView,它有页眉和页脚。问题是我需要项目列表本身比标题更窄。我无法做到这一点。问题是如果我将填充或边距设置为ListView,它将被设置为列出项目,页眉和页脚(由于某种原因,页眉和页脚将比列表项更窄)。仅用于项目布局本身的设置边距是没有用的。以下是布局:
main_layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
header_list_layout.xml:
<?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="match_parent" >
<EditText
android:inputType="textPersonName" />
</RelativeLayout>
footer_list_layout.xml:
<?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="match_parent" >
<TextView
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
list_item_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:textStyle="bold"
android:textColor="@android:color/black"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TextView>
</LinearLayout>
那么我该怎样做才能使一切正常?
图片我希望它看起来如何:
答案 0 :(得分:1)
您可以为ListView
中adapter
的项目设置自定义布局。例如,如果您使用ArrayAdapter
,则可以执行以下操作(请注意getView
方法):
public class MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MySimpleArrayAdapter(Context context, String[] values) {
super(context, R.layout.rowlayout, 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_item, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
textView.setText(values[position]);
// change the icon for Windows and iPhone
String s = values[position];
if (s.startsWith("iPhone")) {
imageView.setImageResource(R.drawable.no);
} else {
imageView.setImageResource(R.drawable.ok);
}
return rowView;
}
}
(从this tutorial复制的代码)
然后在list_item.xml
中,将填充添加到LinearLayout
,您将拥有更窄的视图:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0000ff"
android:paddingLeft="10dp"
android:paddingRight="10dp"
>
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#AAAA00"
/>
<TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00aAAA"
/>
</LinearLayout>
你会得到:
或者您可以将保证金应用于个人TextView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0000ff"
>
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#AAAA00"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
/>
<TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00aAAA"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
/>
</LinearLayout>
你会得到:
或者如果你在布局中有很多子视图,你可以用另一个LinearLayout
包装它(尽管它可能影响性能)并在其上应用保证金
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0000ff"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#AAAAAA"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#AAAA00"
/>
<TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00aAAA"
/>
</LinearLayout>
</LinearLayout>
你会得到: