Gallery正常显示是居中显示,要想设置Gallery向左对齐,可以这么做:
public class GalleryFlow extends Gallery {
private Camera mCamera;
private int mWidth;
private int mPaddingLeft;
private boolean flag;
private static int firstChildWidth;
private static int firstChildPaddingLeft;
private float offsetX;
public GalleryFlow(Context context) {
super(context);
mCamera = new Camera();
this.setStaticTransformationsEnabled(true);
setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
}
public GalleryFlow(Context context, AttributeSet attrs) {
super(context, attrs);
mCamera = new Camera();
setAttributesValue(context, attrs);
this.setStaticTransformationsEnabled(true);
setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
}
public GalleryFlow(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mCamera = new Camera();
setAttributesValue(context, attrs);
this.setStaticTransformationsEnabled(true);
setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
}
private void setAttributesValue(Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs,
new int[] { attr.paddingLeft });
mPaddingLeft = typedArray.getDimensionPixelSize(0, 0);
typedArray.recycle();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
int childrenWidth = getRight() - getLeft();
View sel = getChildAt(0);
int selectedOffset = (childrenWidth / 2) - (sel.getWidth() / 2);
setTranslationX(-selectedOffset);
}
}
这样gallery会看不到右边的图,所以在加进这个gallery的时候要把宽度设大(
或者直接在GalleryFlow的xml里面把layout_width设置成屏幕大小的两倍
)
GalleryFlow gallery = new GalleryFlow(getContext());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(屏幕宽度 * 2,
LinearLayout.LayoutParams.WRAP_CONTENT);
this.addView(gallery,lp);
或者
( <com.eebbk.scrawlpaint.paintlist.GalleryFlow
android:id="@+id/notesGridView"
android:layout_width="2048dp"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:spacing="3pt" >
</com.eebbk.scrawlpaint.paintlist.GalleryFlow>
这是我的一种处理方法,如果对于这种处理有更好的方法,请指教!