如何使用customlayout在布局中心对齐textview?

时间:2014-01-13 04:39:17

标签: android android-layout

我正在研究dragview,因为我想要一个textview,它在中心位置给予(消息点击以便写入),我正在使用以下布局,但textview不会进入中心,它位于布局的顶部,你能建议我吗

以下是我的代码,

<com.snapquote.DragLayer
                    android:id="@+id/drag_layer"
                    android:layout_width="fill_parent"
                    android:layout_centerInParent="true"
                    android:layout_height="fill_parent" >

                    <TextView
                        android:id="@+id/taketext"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="#80313131"
                        android:padding="10.0dip"
                        android:shadowColor="#b3000000"
                        android:shadowDx="0.0"
                        android:gravity="center_vertical"
                        android:shadowDy="-1.0"
                        android:shadowRadius="1.0"
                        android:singleLine="false"
                        android:text="@string/double_tap_to_write"
                        android:textColor="#b3ffffff"
                        android:textSize="16.0sp"
                        android:visibility="visible" />
 </com.snapquote.DragLayer>

类:

   public class MyAbsoluteLayout extends ViewGroup {
public MyAbsoluteLayout(Context context) {
    super(context);
}

public MyAbsoluteLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public MyAbsoluteLayout(Context context, AttributeSet attrs,
        int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int count = getChildCount();

    int maxHeight = 0;
    int maxWidth = 0;

    // Find out how big everyone wants to be
    measureChildren(widthMeasureSpec, heightMeasureSpec);

    // Find rightmost and bottom-most child
    for (int i = 0; i < count; i++) {
        View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            int childRight;
            int childBottom;

            MyAbsoluteLayout.LayoutParams lp
                    = (MyAbsoluteLayout.LayoutParams) child.getLayoutParams();

            childRight = lp.x + child.getMeasuredWidth();
            childBottom = lp.y + child.getMeasuredHeight();

            maxWidth = Math.max(maxWidth, childRight);
            maxHeight = Math.max(maxHeight, childBottom);
        }
    }

    // Account for padding too
    maxWidth += getPaddingLeft () + getPaddingRight ();
    maxHeight += getPaddingTop () + getPaddingBottom ();
    /* original
    maxWidth += mPaddingLeft + mPaddingRight;
    maxHeight += mPaddingTop + mPaddingBottom;
    */

    // Check against minimum height and width
    maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
    maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());

    setMeasuredDimension(resolveSize(maxWidth, widthMeasureSpec),
            resolveSize(maxHeight, heightMeasureSpec));
}

@Override
protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
    return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0, 0);
}

@Override
protected void onLayout(boolean changed, int l, int t,
        int r, int b) {
    int count = getChildCount();

    int paddingL = getPaddingLeft ();
    int paddingT = getPaddingTop ();
    for (int i = 0; i < count; i++) {
        View child = getChildAt(i);
        if (child.getVisibility() != GONE) {

            MyAbsoluteLayout.LayoutParams lp =
                    (MyAbsoluteLayout.LayoutParams) child.getLayoutParams();

            int childLeft = paddingL + lp.x;
            int childTop = paddingT + lp.y;
            /*
            int childLeft = mPaddingLeft + lp.x;
            int childTop = mPaddingTop + lp.y;
            */
            child.layout(childLeft, childTop,
                    childLeft + child.getMeasuredWidth(),
                    childTop + child.getMeasuredHeight());

        }
    }
}

@Override
public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
    return new MyAbsoluteLayout.LayoutParams(getContext(), attrs);
}

// Override to allow type-checking of LayoutParams. 
@Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
    return p instanceof MyAbsoluteLayout.LayoutParams;
}

@Override
protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
    return new LayoutParams(p);
}


public static class LayoutParams extends ViewGroup.LayoutParams {

    public int x;

    public int y;


    public LayoutParams(int width, int height, int x, int y) {
        super(width, height);
        this.x = x;
        this.y = y;
    }

    public LayoutParams(Context c, AttributeSet attrs) {
        super(c, attrs);

    }


    public LayoutParams(ViewGroup.LayoutParams source) {
        super(source);
    }

    public String debug(String output) {
        return output + "Absolute.LayoutParams={width="
                + sizeToString(width) + ", height=" + sizeToString(height)
                + " x=" + x + " y=" + y + "}";
    }


    protected static String sizeToString(int size) {
        if (size == WRAP_CONTENT) {
            return "wrap-content";
        }
        if (size == MATCH_PARENT) {
            return "match-parent";
        }
        return String.valueOf(size);
    }
} // end class

DragLayer.java

      public class DragLayer extends MyAbsoluteLayout 
implements DragSource, DropTarget
{
DragController mDragController;


public DragLayer (Context context, AttributeSet attrs) {
    super(context, attrs);
}

public void setDragController(DragController controller) {
    mDragController = controller;
}

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    return mDragController.dispatchKeyEvent(event) || super.dispatchKeyEvent(event);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    return mDragController.onInterceptTouchEvent(ev);
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    return mDragController.onTouchEvent(ev);
}

@Override
public boolean dispatchUnhandledMove(View focused, int direction) {
    return mDragController.dispatchUnhandledMove(focused, direction);
}

public boolean allowDrag () {
// In this simple demo, any view that you touch can be dragged.
return true;
}


public void onDropCompleted (View target, boolean success)
{
 toast ("DragLayer2.onDropCompleted: " + target.getId () + " Check that the view moved.");
}


public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
    DragView dragView, Object dragInfo)
{
View v = (View) dragInfo;
toast ("DragLayer2.onDrop accepts view: " + v.getId ()
      + "x, y, xO, yO :" + new Integer (x) + ", " + new Integer (y) + ", "
      + new Integer (xOffset) + ", " + new Integer (yOffset));

int w = v.getWidth ();
int h = v.getHeight ();
int left = x - xOffset;
int top = y - yOffset;
DragLayer.LayoutParams lp = new DragLayer.LayoutParams (w, h, left, top);
this.updateViewLayout(v, lp);
}

public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
    DragView dragView, Object dragInfo)
{
}

public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
    DragView dragView, Object dragInfo)
{
}

 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
    DragView dragView, Object dragInfo)
{
}


public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
    DragView dragView, Object dragInfo)
{
return true;
}

 public Rect estimateDropLocation(DragSource source, int x, int y, int xOffset, int yOffset,
        DragView dragView, Object dragInfo, Rect recycle)
{
return null;
}

 public void toast (String msg)
 {
 if (!DragActivity.Debugging) return;
 Toast.makeText (getContext (), msg, Toast.LENGTH_SHORT).show ();
 } // end toast


} // end class

2 个答案:

答案 0 :(得分:1)

在父布局

中使用以下属性
  android:layout_gravity="center"
  android:gravity="center"

注意:API level 3中已弃用AbsoluteLayout类。

改为使用FrameLayout, RelativeLayoutcustom layout

您需要添加几行的MyAbsoluteLayout.java onLayout方法

修改

 // Use the child's gravity and size to determine its final
// frame within its container.

  Gravity.apply(lp.gravity, width, height, mTmpContainerRect, mTmpChildRect);

转到thru this link并进行一些更改,以便为自定义布局应用重力

答案 1 :(得分:0)

AbsoluteLayout已折旧,您应该使用FrameLayout,LinearLayout或RelativeLayout。

如果您使用RelativeLayout,请将以下内容添加到TextView:

android:layout_centerInParent="true"

对于FrameLayout或LinearLayout,将以下内容添加到父级:

android:gravity="center"