gpt4 book ai didi

android - 如何使用自定义布局在布局中心对齐 TextView ?

转载 作者:行者123 更新时间:2023-11-30 03:10:27 25 4
gpt4 key购买 nike

我正在开发 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>

类(class):

   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"

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

改用FrameLayout、RelativeLayout自定义布局

编辑:在您的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并进行一些更改以将重力应用到您的自定义布局

关于android - 如何使用自定义布局在布局中心对齐 TextView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21083995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com