1

我想创建一个简单的图像布局,可以像表格一样在列和行中查看它。有谁知道这是否可能

4

2 回答 2

0

尝试如下:

布局文件:

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
       android:layout_width="match_parent"
        android:layout_height="match_parent"
     tools:context=".MainActivity" >
<GridView
    android:id="@+id/gridView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:columnWidth="90dp"
    android:gravity="center"
    android:horizontalSpacing="10dp"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:verticalSpacing="10dp" 
    android:layout_below="@+id/Linearlayout">
</GridView>
<LinearLayout
    android:id="@+id/Linearlayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:text="Button" />
    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>
<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/gridView1"
    android:text="TextView" />
  </RelativeLayout>

适配器类

     public class ImageAdapter extends BaseAdapter {
  private Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = {
    R.drawable.ic_launcher, R.drawable.ic_launcher,
        R.drawable.ic_launcher, R.drawable.ic_launcher,
        R.drawable.ic_launcher, R.drawable.ic_launcher,
        R.drawable.ic_launcher, R.drawable.ic_launcher
};
// Constructor
public ImageAdapter(Context c) {
    mContext = c;
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return mThumbIds.length;
}
@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return mThumbIds[position];
}
@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}
@Override
public View getView(int position, View arg1, ViewGroup arg2) {
    // TODO Auto-generated method stub
    ImageView imageView = new ImageView(mContext);
    imageView.setImageResource(mThumbIds[position]);
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
    return imageView;
}
}

调用适配器类将数据绑定到 GridView 中,如下所示在您的活动中

 public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    GridView m_grid=(GridView)findViewById(R.id.gridView1);
    m_grid.setAdapter(new ImageAdapter(getApplicationContext()));
}

}

于 2013-09-06T05:49:09.650 回答
0

网格视图代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:columnWidth="90dp"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp" >
    </GridView>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Button" />

</RelativeLayout>

图像适配器:

public class ImageAdapter extends BaseAdapter {
    private Context mContext;


 // Keep all Images in array
    public Integer[] mThumbIds = {

            //R.drawable.1
        R.drawable.a,R.drawable.b,
        R.drawable.c,R.drawable.d,
        R.drawable.g,R.drawable.f,
        R.drawable.h,R.drawable.i,
        R.drawable.j,R.drawable.k,
        R.drawable.l,R.drawable.m

    };

    // Constructor
    public ImageAdapter(Context c){
        mContext = c;
    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mThumbIds.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return mThumbIds[position];
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub

         ImageView imageView = new ImageView(mContext);
            imageView.setImageResource(mThumbIds[position]);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
            return imageView;


    }


}

主要活动:

 @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
              GridView gridView = (GridView) findViewById(R.id.gridView1);
              Button btnNavigate=(Button)findViewById(R.id.button1);

              btnNavigate.setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Intent intent = new Intent(getBaseContext(), SpinnerDemo.class);
                    startActivity(intent);
                }
            });
                // Instance of ImageAdapter Class
                gridView.setAdapter(new ImageAdapter(this));
        }

androidhive.info 上提供了许多演示

于 2013-09-06T05:30:41.950 回答