gpt4 book ai didi

java - 使 ListView 占满整个屏幕

转载 作者:行者123 更新时间:2023-11-29 05:44:01 25 4
gpt4 key购买 nike

我有一个 ListView ,我想填满整个屏幕, ListView 中有四个项目。填充四个项目后,它在下面留下空白空间。您可以在屏幕截图中看到。它留下了空白。我想要覆盖整个屏幕。

enter image description here

我想要这样:

enter image description here

这是源码

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);



}

}

自定义适配器.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 很奇怪,也没有必要。考虑您的约束(例如:如果项目溢出怎么办?)并使用适当的布局管理器。就像垂直 LinearLayout 一样,最后一项的布局权重不为零。

ListView 仅在您需要动态生成列表项的抽象时才有意义。

关于java - 使 ListView 占满整个屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16448865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com