尝试制作一个可滚动的高分列表,如下所示:
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 以允许添加不同大小的列?如果是,那么我的做法好吗? (可能不是,因为它不起作用 :))我是不是错过了什么?
提前致谢!
最佳答案
我们不得不为一个项目做这个,并且不想使用 GridView
以外的任何东西,因为功能是重复的,但是在一个 GridView
上我们需要稍微不同的 View 配置(但仍然使用适配器和所有其他好东西)。我们发现,如果您重写 GridView
的 layoutChildren
函数,您就可以做到这一点,尽管它的文档很少。
我们的实现检查特殊 View 以及新布局是否已经实现:
@Override
protected void layoutChildren(){
super.layoutChildren();
if(!isSpecial || layoutAlreadySet) return;
layoutAlreadySet = true;
//Implement special layout....
}
关于android - 具有不同列大小的 GridView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6651175/