可以创建LinearLayouts的GridVIew吗?

Nul*_*ion 4 android gridview

我需要有一个linearlayout网格视图.每个linearLayout必须有一个imageview和一个相对布局的孩子,上面有更多的孩子.

我正在寻找创建LinearLayouts网格视图的教程/示例,但我什么都找不到.

有人有教程或可以给我一些例子或帮助做到这一点?

谢谢

Ale*_*s G 9

是的,这是可能的,而且非常简单.使用GridView时,请为其提供适配器.在适配器的getview方法中,您可以创建任何您喜欢的视图并将其返回.例如,您可以从XML中扩展视图 - 并且该xml可能包含一个LinearLayout.或者,您可以在该方法中动态创建线性布局,并向其中添加其他组件.

在Google上查看这篇文章:http://developer.android.com/resources/tutorials/views/hello-gridview.html

更新:一个小例子

在你的 res/layout/item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:paddingTop="0dip"
     android:paddingBottom="0dip"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal">

    <TextView android:id="@+id/TxtName"
         android:scrollHorizontally="false"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:textColor="@android:color/black"
         android:layout_weight="0.2"
         android:padding="2dp"/>

     <TextView android:id="@+id/TxtPackage"
         android:scrollHorizontally="false"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_weight="0.2"
         android:textColor="@android:color/black"
         android:padding="2dp"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

然后在你的适配器:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    //get the item corresponding to your position

    LinearLayout row = (LinearLayout) (convertView == null
               ? LayoutInflater.from(context).inflate(R.layout.item, parent, false)
               : convertView);
    ((TextView)row.findViewById(R.id.TxtName)).setText("first text");
    ((TextView)row.findViewById(R.id.TxtPackage)).setText("second text");
    return row;
}
Run Code Online (Sandbox Code Playgroud)