ViewGroup中添加Gallery图片显示不全
直接贴代码:Activity:
package com.example.testviewgroup;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
MyViewGroup myViewGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myViewGroup=new MyViewGroup(this);
Button btn_setView=(Button) this.findViewById(R.id.btn_setView);
btn_setView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MainActivity.this.setContentView(myViewGroup);
}
});
}
}
ViewGroup:
package com.example.testviewgroup;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
public class MyViewGroup extends ViewGroup {
Gallery myGallery;
public MyViewGroup(Context context) {
super(context);
setBackgroundColor(Color.BLACK);
myGallery=new Gallery(this.getContext());
myGallery.setBackgroundColor(Color.BLUE);
myGallery.setAdapter(new ImageAdapter(this.getContext()));
addView(myGallery);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
myGallery.layout(0, 0, 400, 400);
}
public class ImageAdapter extends BaseAdapter {
public ImageAdapter(Context context) {
mContext = context;
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setBackgroundColor(Color.RED);
i.setImageResource(mImageIds[position]);
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setLayoutParams(new Gallery.LayoutParams(200, 200));
return i;
}
private Context mContext;
private Integer[] mImageIds = {
R.drawable.gallery_photo_1,
R.drawable.gallery_photo_2,
R.drawable.gallery_photo_3,
R.drawable.gallery_photo_4,
R.drawable.gallery_photo_5,
R.drawable.gallery_photo_6,
R.drawable.gallery_photo_7,
R.drawable.gallery_photo_8
};
}
}
Gallery中显示出来的效果呢,是图片只显示下半部分,上半部分在Gallery之外不能显示。不知道如何解决这个问题。
如图 ViewGroup Gallery 图片 显示不全
补充:移动开发 , Android