簡體   English   中英

如何使用customlayout在布局中心對齊textview?

[英]How to align textview in layout center using customlayout?

我正在為dragview進行工作,因為我想要一個文本視圖,該文本視圖在居中位置是給定的(點擊以寫消息),我正在使用以下布局,但是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

在父級布局中的屬性下面使用此屬性

  android:layout_gravity="center"
  android:gravity="center"

注意:在API級別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);

通過此鏈接進行一些更改,以將重力應用於您的自定義布局

AbsoluteLayout已貶值,您應該真正使用FrameLayout,LinearLayout或RelativeLayout。

如果使用RelativeLayout,請將以下內容添加到TextView中:

android:layout_centerInParent="true"

對於FrameLayout或LinearLayout,將以下內容添加到父項:

android:gravity="center"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM