簡體   English   中英

如何使線性布局出現在gridview中?

[英]How do I make a linear layout appear in a gridview?

這是代碼。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/grid_cell_color"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#80A9A9A9"
        android:orientation="vertical" >
    </LinearLayout>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/grid_cell_border" />

    <RelativeLayout
        android:id="@+id/grid_cell_background"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </RelativeLayout>

</FrameLayout>

適配器代碼為:

    @Override
    public int getCount() {
        return titles.length;
    }

    @Override
    public Object getItem(int position) {
        return titles[position];
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

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

        if (inflater == null)
            inflater = (LayoutInflater) view.getContext().getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);

        row = inflater.inflate(R.layout.grid_view_single_cell, parent, false);

        LinearLayout gridCell1= (LinearLayout) row.findViewById(R.id.grid_cell_color);

        if(position == 0 || position == 1 || position == 5){
            gridCell1.setVisibility(View.GONE);
            Log.i("GRID", "ENTERED");
        }

        RelativeLayout gridCell = (RelativeLayout) row.findViewById(R.id.grid_cell_background);

        gridCell.setBackgroundResource(titles[position]);
        return row;
    }


}

我想在某些單元格中放置一個灰色的線性布局。 但是它沒有出現。 我該怎么做? 我的參考文獻似乎正確,但是灰色布局仍然沒有顯示。

原因是ListView的項目將被重用。

此代碼后:

if(position == 0 || position == 1 || position == 5){
     gridCell1.setVisibility(View.GONE);
     Log.i("GRID", "ENTERED");
    }

您應該添加else子句:

else{
    gridCell1.setVisibility(View.VISIBLE);
}

可能還有另一個原因:將Linearlayout的高度更改為1dp。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM