使列表视图占据整个屏幕
我有列表视图,我想填充整个屏幕,有四个项目在列表视图。四个项目填满后,它会留下空的空间。您可以在屏幕截图中看到。它留下了空白空间。我希望整个屏幕被覆盖。使列表视图占据整个屏幕
我想有这样的:
这里是源代码
MainActivity.java。
public class MainActivity extends Activity {
ListView resultPane;
List<Taskinfo> list;
CustomAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultPane = (ListView) findViewById(R.id.mylist);
list = new ArrayList<Taskinfo>();
Resources res = getResources(); // resource handle
Drawable drawable = res.getDrawable(R.drawable.browse_home);
list.add(new Taskinfo("Browse", drawable));
drawable = res.getDrawable(R.drawable.jewelry);
list.add(new Taskinfo("Whats New", drawable));
drawable = res.getDrawable(R.drawable.show);
list.add(new Taskinfo("Upcoming Show", drawable));
drawable = res.getDrawable(R.drawable.contact);
list.add(new Taskinfo("Contact Us", drawable));
adapter = new CustomAdapter(this, list);
resultPane.setAdapter(adapter);
}
}
CustomAdapter.java
public class CustomAdapter extends BaseAdapter {
private Context context;
private List<Taskinfo> list;
public CustomAdapter(Context context, List<Taskinfo> list) {
this.context = context;
this.list = list;
}
public View getView(int index, View view, final ViewGroup parent) {
if (view == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
view = inflater.inflate(R.layout.single_list_item, parent, false);
}
Taskinfo t = list.get(index);
RelativeLayout l = (RelativeLayout) view
.findViewById(R.id.testrelative);
l.setBackgroundDrawable(t.getImage());
TextView textView = (TextView) view.findViewById(R.id.title);
textView.setText(t.getName());
return view;
}
}
single_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/testrelative"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/browse_home"
android:orientation="horizontal"
android:padding="5dip" >
<!-- ListRow Left sied Thumbnail image -->
<LinearLayout
android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip"
android:padding="3dip" >
</LinearLayout>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="16dp"
android:text=""
android:textColor="#040404"
android:textSize="15dip"
android:textStyle="bold"
android:typeface="sans" />
</RelativeLayout>
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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="45sp"
android:id="@+id/llayout"
android:background="@drawable/navbar"
android:padding="3sp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Golden Stone"
android:textColor="@color/white"
android:textSize="20sp" >
</TextView>
</LinearLayout>
<ListView
android:id="@+id/mylist"
android:layout_width="match_parent"
android:layout_below="@id/llayout"
android:layout_height="match_parent" >
</ListView>
</RelativeLayout>
你会想设置ListView的背景。目前,您的视图应该到底,但ListView的背景是透明的,因此将其设置为白色应该可以实现您所要求的。
<ListView ...
android:background="@android:color/white"
... />
不添加背景,因为白色将不起作用。它只是开始新的项目。你可以在这里查看http://img28.imageshack.us/img28/1822/device20130509011734.png – 2013-05-08 19:49:40
它不会启动一个新的列表项 - 但它确实可以防止列表在它应该出现之前结束。但是因为您似乎希望将4个项目均匀分布,请采用fdreger的建议并使用带有权重的LinearLayout。 – 2013-05-08 19:53:25
在这种情况下使用ListView
是奇怪的和不必要的。考虑一下你的约束条件(比如:如果项目溢出?),并使用适当的布局管理器。与垂直LinearLayout
一样,最后一项的布局权重不为零。
ListView
奇妆感只有当你需要一台即时生成列表项的抽象。
我现在有这个问题。客户想要这样,所以我必须这样实施。你会进一步描述你的建议吗? – 2013-05-08 19:51:32
@TonyChhabada:你没有道理。你的问题源于你正在尝试做一些'LinearLayout'完美的事情(可能包装到ScrollView中),但出于某种奇怪的原因,坚持要做一些完全相同的事情,但需要一些疯狂的'ListView'组合。 – fdreger 2013-05-08 20:05:25
@TonyChhabada:忽略所有适配器,列表视图和内容。只要做一个线性布局,四次膨胀你的single_row,放入适当的drawable并添加到布局中。当你添加最后一个时,设置一些布局权重。完成。 – fdreger 2013-05-08 20:14:26
如果有这么几个项目,你可以使用的LinearLayout代替,并给予的权重为它的每个项目。
然而,这样做请注意,Android支持多种设备和屏幕上,所以你可能想拥有的每一行会多小的限制。
无论如何,如果你想使每一行的安装高度,你可以检查其大小,然后通过项目的数量划分。
,以获得列表视图的大小,你可以使用this small snippet我所做的。
据我所知,你需要设置列表中视图的高度来适应屏幕,如果是的话,这将通过代码 – 2013-05-08 19:43:06
完成,使用linearlayout并让它适合整个屏幕 – 2013-05-08 19:43:34
谢谢@MoshErsan有效。我使用代码根据屏幕大小动态分配高度。 – 2013-05-08 21:05:08