是否可以创建自定义的图像视图?

时间:2013-09-06 05:18:26

标签: java android

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

2 个答案:

答案 0 :(得分: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>

ImageAdapter:

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;


    }


}

<强> MainActivity:

 @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上有很多演示

答案 1 :(得分: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>

<强> AdapterClass

     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()));
}
     

}