具有不同列大小的

具有不同列大小的

本文介绍了具有不同列大小的 GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试制作一个可滚动的高分列表,如下所示:

Trying to make a highscore list, which is scrollable, and looks like this:

foo         1000
bar         876
foobar      500
foobarfoo   1

我目前正在使用 GridView 进行此操作.我想将名称列宽度设置为屏幕的 60%,将分数列宽度设置为 40%.可能吗?

I am currently doing it with a GridView. I would like to set the name column width to 60% of the screen and the score column width to 40%. Is it possible?

目前我正在尝试通过服装适配器.这是它的 getview 函数:

Currently I am trying via a costum adapter. Here is the getview funcion for it:

public View getView(int position, View convertView, ViewGroup parent) {
    TextView tv;
    if (convertView == null) {
        tv = new TextView(context);
        tv.setTextSize(25);
        tv.setTextColor(Color.WHITE);

        if (position % 2 == 0)
        {
            tv.setLayoutParams(new GridView.LayoutParams((width/10)*6, 50));
        }
        else
        {
            tv.setLayoutParams(new GridView.LayoutParams((width/10)*4, 50));
        }
    }
    else {
        tv = (TextView) convertView;
    }

    tv.setText(texts[position]);
    return tv;
}

布局由 gridview 和底部的按钮构建.XML 文件是:

The layout is built by the gridview and a button at the bottom. The XML file is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:orientation="vertical" android:id="@+id/top">
    <GridView android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_weight="2"
              android:id="@+id/grid"
              android:numColumns="2"
              android:columnWidth="0dp"
              >
    </GridView>
    <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/backbutton" android:text="@string/backstr"></Button>

</LinearLayout>

那么问题又来了:是否可以设置 GridView 以允许添加不同大小的列?如果是,那我的方法好不好?(可能不是,因为它不起作用:))我是不是错过了什么?

So the question again: Is it possible to set the GridView to let adding different sized columns? If yes, then my approach is good? (Probably not, since it is not working :)) Did I just miss something?

先谢谢你!

推荐答案

工作魅力十足!非常感谢 Abhinav,他提出了很好的建议.这是每个遇到相同问题的人的代码:

Working like a charm! Big thanks to Abhinav, who made a good advise. Here is the code for everyone who has the same problem:

public View getView(int position, View convertView, ViewGroup parent) {
    TextView tv1;
    TextView tv2;
    LinearLayout ll;
    if (convertView == null) {
        tv1 = new TextView(context);
        tv1.setTextSize(25);
        tv1.setTextColor(Color.WHITE);
        tv1.setGravity(Gravity.LEFT);
        tv1.setPadding(5, 5, 5, 5);
        tv1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
                                                          LinearLayout.LayoutParams.FILL_PARENT,
                                                          (float) 3.0));

        tv2 = new TextView(context);
        tv2.setTextSize(25);
        tv2.setTextColor(Color.WHITE);
        tv2.setGravity(Gravity.RIGHT);
        tv2.setPadding(5, 5, 5, 5);
        tv2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
                                                              LinearLayout.LayoutParams.FILL_PARENT,
                                                              (float) 4.0));

        ll = new LinearLayout(context);
        ll.setOrientation(0);
        ll.setPadding(5, 5, 5, 10);

        tv1.setText(names[position]);
        tv2.setText(scores[position]);

        ll.addView(tv1);
        ll.addView(tv2);
    }
    else {
        ll = (LinearLayout) convertView;
        tv1 = (TextView) ll.getChildAt(0);
        tv2 = (TextView) ll.getChildAt(1);

        tv1.setText(names[position]);
        tv2.setText(scores[position]);
    }

    return ll;
}

和 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:orientation="vertical" android:id="@+id/top">
    <ListView android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_weight="2"
              android:numColumns="2"
              android:columnWidth="0dp"
              android:id="@+id/list">
    </ListView>
    <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/backbutton" android:text="@string/back"></Button>

</LinearLayout>

如果你认为你可以改进这个代码(因为我是这个领域的新手)不要犹豫,回复!提前致谢!

If you think you can improve this code (since I am a newbie in this field) don't hesitate, to reply! Thanks in advance!

这篇关于具有不同列大小的 GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:14