gpt4 book ai didi

java - 是否可以创建自定义 ImageView ?

转载 作者:行者123 更新时间:2023-12-01 04:24:00 25 4
gpt4 key购买 nike

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

最佳答案

GridView 代码:

<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;


}


}

主要 Activity :

 @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 上提供了许多演示

关于java - 是否可以创建自定义 ImageView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18650406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com