6

我正在使用一个 GridView,它显示一个带有背景图像和文本的线性布局,就像这个例子一样:

http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners/

就像那个例子一样,我正在使用该代码使图像具有圆角。

问题是,如果我不使用圆角的东西,我所有的网格项目都是相同的高度,无论它们里面有什么文本,即使我所有的图像都是相同的大小。

但如果我使用它,项目高度将被包裹到内容中。

我尝试将我的线性布局定义为 wrap_content、fill_parent、match_parent 甚至固定高度,但系统只是忽略了我。

这是我的网格布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFDDDDDD"
    android:orientation="vertical"
    tools:context=".RouteListActivity" >

    <GridView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gridview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_margin="20dp"
        android:layout_weight="2"
        android:animateLayoutChanges="false"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:drawSelectorOnTop="true"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:numColumns="2"
        android:scrollingCache="false"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp" />

</LinearLayout>

这是我的网格项目:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearlayoutGridLabel"
    android:layout_width="wrap_content"
    android:layout_height="150dp"
    android:layout_weight="1"
    android:gravity="bottom|left"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/id_ruta"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:visibility="gone" />

    <TextView
        android:id="@+id/routenameGridLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:background="#77000000"
        android:gravity="bottom"
        android:padding="5dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

加上我在适配器中的 getView:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.grid_item, null);

        LinearLayout linLayout = (LinearLayout) vi.findViewById(R.id.linearlayoutGridLabel);
        Typeface yanone = Typeface.createFromAsset(mContext.getAssets(), "YanoneKaffeesatz-Regular.ttf");
        TextView id_ruta = (TextView) vi.findViewById(R.id.id_ruta);
        TextView nombre=(TextView)vi.findViewById(R.id.routenameGridLabel);
        nombre.setTypeface(yanone);
        nombre.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 38);
        Ruta current = routeList.get(position);
        try {
//          Drawable d;
//          d = Drawable.createFromStream(mContext.getAssets().open("ruta" + (position+1) + "/title.jpg"), null);
            Bitmap b = BitmapFactory.decodeStream(mContext.getAssets().open("ruta" + (position+1) + "/title.jpg"));
            StreamDrawable d = new StreamDrawable(b, 10, 0);
            linLayout.setBackgroundDrawable(d);
        } catch (IOException e) {
            linLayout.setBackgroundResource(R.drawable.noimage);
        }
        nombre.setText("" + current.getNombre());
        id_ruta.setText(current.getId().toString());

        return vi;
    }

坦率地说,我不理解 gridviews 的行为。以前我将不同大小的图像设置为背景图像,并且所有元素相互重叠。即使在网格项目的布局上固定高度,我也无法制作相同大小的行。

请问这里可以指路吗?

4

2 回答 2

35

我终于解决了,原来问题在于我正在使用

vi = inflater.inflate(R.layout.grid_item, null);

代替

vi = inflater.inflate(R.layout.grid_item, parent, false);

所以布局参数被忽略了。

无论如何,让 GridView 均匀分布空间是一件很痛苦的事情。我想我将切换到 TableView。

于 2013-01-05T10:52:47.560 回答
1

您可以使用 setLayoutParams 设置布局高度:

vi.setLayoutParams(new GridView.LayoutParams(<width_in_pixels>, <height_in_pixels>));

您需要以像素为单位传递宽度和高度。因此,您可能希望将 150dp 转换为像素。

于 2021-02-09T09:45:04.233 回答