android listview 行占据整个高度

发布于 2024-12-27 19:31:16 字数 4953 浏览 2 评论 0原文

我有一个与列表视图一起使用的相对布局视图。由于某种原因,即使我已将 layout_height 设置为 wrap_content,布局仍占用了它所能占用的所有空间。结果是列表中的每一行都与屏幕一样高!

我不确定这个观点有什么问题。它曾经工作得很好。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/cloudbkg" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/pay" />

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/imageView1"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:singleLine="true"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>

</RelativeLayout>

这是 getView 函数。

public View getView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                try {
                    final JSONObject holder = getItem(position);
                    convertView = inflateView(R.layout.dumb);
                    convertView.setOnClickListener(new OnClickListener() {
                        public void onClick(View v) {
                            try {
                                Intent i = new Intent(context, Pending.class);
                                i.putExtra("id", holder.getString("_id"));
                                context.startActivity(i);
                            } catch(Throwable e){
                                e.printStackTrace();
                            }
                        }
                    });
                    convertView.setOnLongClickListener(new OnLongClickListener(){
                        public boolean onLongClick(View v){
                            try {
                                final CharSequence[] items = {"Pay", "Delete"};
                                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                                builder.setTitle(holder.getString("service"));
                                builder.setItems(items, new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int item) {

                                    }
                                }).create().show();
                            } catch(Throwable e){
                                e.printStackTrace();
                            }
                            return true;
                        }
                    });
                    TextView t = (TextView)convertView.findViewById(R.id.amount);
                    t.setText(holder.getString("currency") + holder.getString("amount"));
                    setImg((ImageView)convertView.findViewById(R.id.pic), holder.getJSONObject("to").getString("pic"));
                    t = (TextView) convertView.findViewById(R.id.time);
                    t.setText(DateUtils.getRelativeTimeSpanString(holder.getLong("time")*1000, System.currentTimeMillis(), 0, DateUtils.FORMAT_ABBREV_ALL));
                    t = (TextView) convertView.findViewById(R.id.service);
                    t.setText(holder.getString("service")); // url, co-ords, or shop

                } catch(Throwable e) {
                    e.printStackTrace();
                }
            }
            return convertView;
        }

I have a relativelayout view for use with a listview. For some reason, even though I have set layout_height to wrap_content, the layout is taking up all the space it can. The result is that every row in the list is as tall as the screen!

I am not sure what is wrong with the view. It used to work just fine.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/cloudbkg" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/pay" />

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/imageView1"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:singleLine="true"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>

</RelativeLayout>

Here is the getView function.

public View getView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                try {
                    final JSONObject holder = getItem(position);
                    convertView = inflateView(R.layout.dumb);
                    convertView.setOnClickListener(new OnClickListener() {
                        public void onClick(View v) {
                            try {
                                Intent i = new Intent(context, Pending.class);
                                i.putExtra("id", holder.getString("_id"));
                                context.startActivity(i);
                            } catch(Throwable e){
                                e.printStackTrace();
                            }
                        }
                    });
                    convertView.setOnLongClickListener(new OnLongClickListener(){
                        public boolean onLongClick(View v){
                            try {
                                final CharSequence[] items = {"Pay", "Delete"};
                                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                                builder.setTitle(holder.getString("service"));
                                builder.setItems(items, new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int item) {

                                    }
                                }).create().show();
                            } catch(Throwable e){
                                e.printStackTrace();
                            }
                            return true;
                        }
                    });
                    TextView t = (TextView)convertView.findViewById(R.id.amount);
                    t.setText(holder.getString("currency") + holder.getString("amount"));
                    setImg((ImageView)convertView.findViewById(R.id.pic), holder.getJSONObject("to").getString("pic"));
                    t = (TextView) convertView.findViewById(R.id.time);
                    t.setText(DateUtils.getRelativeTimeSpanString(holder.getLong("time")*1000, System.currentTimeMillis(), 0, DateUtils.FORMAT_ABBREV_ALL));
                    t = (TextView) convertView.findViewById(R.id.service);
                    t.setText(holder.getString("service")); // url, co-ords, or shop

                } catch(Throwable e) {
                    e.printStackTrace();
                }
            }
            return convertView;
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一城柳絮吹成雪 2025-01-03 19:31:16

确保您使用的两个图像不太大。

Make sure the two images you use are not too large.

看透却不说透 2025-01-03 19:31:16

不确定这是否是问题所在,但当您创建自定义 ListActivity 布局时,您需要设置列表的屏幕布局以及单个行的布局。确保您传递给 setContentView() 的资源文件包含 ID 为 android:id/listListView 对象,并且您传递的是行布局(如您上面发布的)到适当的适配器。

Not sure if this is the issue, but when you create a custom ListActivity layout, you need to set the list's screen layout as well as the layout of an individual row. Ensure that the resource file you pass to setContentView() contains a ListView object with the id android:id/list and that you are passing the row layout (as you posted above) to the appropriate adapter.

起风了 2025-01-03 19:31:16

如果您知道该行大约需要多高(例如:60dp),那么不要在根RelativeLayout中给出wrap_content,而是给出layout_height="60dp"。然后,您可以使用 layout_centerVertical 正确对齐视图。

如果这是不可接受的,请尝试为不同的视图提供不同的背景颜色,作为调试并查看哪个视图负责占用所有空间的方法。

If you know how tall the row would take approximately(eg: 60dp), then instead of giving wrap_content give layout_height="60dp" in the root RelativeLayout. You can then use layout_centerVertical to correctly align the views.

If that is not acceptable try giving different background colors to different views as a way to debug and see which view is responsible for taking all the space.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文