向左对齐的Gallery

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>

这是我的一种处理方法,如果对于这种处理有更好的方法,请指教!



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值