用于:
然后我确实不喜欢GridView的设计所以我使用了here的功能,我跟着kcoppock的回答调整了一下,最后我得到了这个结果
立即
布局完全覆盖了状态栏!!!什么......
我认为这个问题可能在XML文件中,但我无法弄清楚。
包含GridView的fragment2.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical" >
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2"
android:verticalSpacing="1dp"
android:horizontalSpacing="1dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</RelativeLayout>
grid_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<my.com.impressor.demo2.SquareImageView
android:id="@+id/picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
</RelativeLayout>
Fragment2.java
public class Fragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment2, container, false);
GridView gridView = (GridView) view.findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(getActivity().getApplicationContext()));
//Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.p1);
return view;
}
}
与grid_item.xml相关的SquareImageView
public class SquareImageView extends ImageView {
public SquareImageView(Context context) {
super(context);
}
public SquareImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); //Snap to width
}
}
ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
private final List<Item> mItems = new ArrayList<Item>();
private final LayoutInflater mInflater;
public ImageAdapter(Context context) {
mInflater = LayoutInflater.from(context);
/*mItems.add(new Item("Red", R.drawable.t1));
mItems.add(new Item("Magenta", R.drawable.t2));
mItems.add(new Item("Dark Gray", R.drawable.t3));
mItems.add(new Item("Gray", R.drawable.t4));
mItems.add(new Item("Green", R.drawable.t5));
mItems.add(new Item("Cyan", R.drawable.t6));*/ //end here
mItems.add(new Item(R.drawable.t1));
mItems.add(new Item(R.drawable.t2));
mItems.add(new Item(R.drawable.t3));
mItems.add(new Item(R.drawable.t4));
mItems.add(new Item(R.drawable.t5));
mItems.add(new Item(R.drawable.t6));
mItems.add(new Item(R.drawable.t7));
mItems.add(new Item(R.drawable.t8));
mItems.add(new Item(R.drawable.t9));
mItems.add(new Item(R.drawable.t10));
mItems.add(new Item(R.drawable.t11));
mItems.add(new Item(R.drawable.t12));
mItems.add(new Item(R.drawable.t13));
mItems.add(new Item(R.drawable.t14));
mItems.add(new Item(R.drawable.t15));
mItems.add(new Item(R.drawable.t16));
mItems.add(new Item(R.drawable.t17));
mItems.add(new Item(R.drawable.t18));
mItems.add(new Item(R.drawable.t19));
mItems.add(new Item(R.drawable.t20));
mItems.add(new Item(R.drawable.t21));
mItems.add(new Item(R.drawable.t22));
}
@Override
public int getCount() {
return mItems.size();
}
@Override
public Item getItem(int i) {
return mItems.get(i);
}
@Override
public long getItemId(int i) {
return mItems.get(i).drawableId;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v = view;
ImageView picture;
//TextView name;
if (v == null) {
v = mInflater.inflate(R.layout.grid_item, viewGroup, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
//v.setTag(R.id.text, v.findViewById(R.id.text));
}
picture = (ImageView) v.getTag(R.id.picture);
//name = (TextView) v.getTag(R.id.text);
Item item = getItem(i);
picture.setImageResource(item.drawableId);
//name.setText(item.name);
return v;
}
private static class Item {
//public final String name;
public final int drawableId;
Item(int drawableId) {
//this.name = name;
this.drawableId = drawableId;
}
}
}
答案 0 :(得分:0)
您应该RecyclerView
使用GridLayoutManager
代替GridView
。这将解决您的所有问题并提高灵活性。