【发布时间】:2011-10-02 19:56:40
【问题描述】:
尝试制作一个可滚动的高分列表,如下所示:
foo 1000
bar 876
foobar 500
foobarfoo 1
我目前正在使用 GridView。我想将名称列宽设置为屏幕的 60%,将分数列宽设置为 40%。有可能吗?
目前我正在尝试通过服装适配器。这是它的 getview 函数:
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 文件是:
<?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 设置为允许添加不同大小的列?如果是,那么我的方法好吗? (可能不是,因为它不起作用:))我错过了什么吗?
提前谢谢你!
【问题讨论】:
-
您可以尝试将列宽设置为 wrap_content 而不是 0dp,然后看看它是否有效?
-
遗憾的是,这不是列宽的有效值。但是感谢您的评论!
-
在这种情况下,您可以使用 ListView 而不是 GridView,并且对于每一行,使用您在适配器的 getView 方法中填充的具有两个文本视图(最好是线性布局)的布局。
-
用这个方法可以滚动列表吗?
-
是的,ListViews 是可滚动的 :)