如何在ViewGroup Android的右上方放置一个按钮?

时间:2018-08-30 09:22:23

标签: android android-layout

enter image description here

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.testlayout.MyViewGroup
        android:id="@+id/parent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true">
        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="top|right"

            android:text="Hide" />

    </com.testlayout.MyViewGroup>
</RelativeLayout>

PercentFrameLayout.java

public class MyViewGroup extends ViewGroup {
    private int xPercent = 0;
    private int yPercent = 0;
    private int widthPercent = 100;
    private int heightPercent = 100;

    public MyViewGroup(Context context) {
        super(context);
    }

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

    public MyViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    private RectF mBorderRect = new RectF();
    Paint mBgPaint = new Paint();

    public void setPosition(int xPercent, int yPercent, int widthPercent, int heightPercent) {
        this.xPercent = xPercent;
        this.yPercent = yPercent;
        this.widthPercent = widthPercent;
        this.heightPercent = heightPercent;
    }

    @Override
    public boolean shouldDelayChildPressedState() {
        return false;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final int width = getDefaultSize(Integer.MAX_VALUE, widthMeasureSpec);
        final int height = getDefaultSize(Integer.MAX_VALUE, heightMeasureSpec);
        setMeasuredDimension(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));

        final int childWidthMeasureSpec =
                MeasureSpec.makeMeasureSpec(width * widthPercent / 100, MeasureSpec.AT_MOST);
        final int childHeightMeasureSpec =
                MeasureSpec.makeMeasureSpec(height * heightPercent / 100, MeasureSpec.AT_MOST);
        for (int i = 0; i < getChildCount(); ++i) {
            final View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
            }
        }
    }


    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        final int width = right - left;
        final int height = bottom - top;
        // Sub-rectangle specified by percentage values.
        final int subWidth = width * widthPercent / 100;
        final int subHeight = height * heightPercent / 100;
        final int subLeft = left + width * xPercent / 100;
        final int subTop = top + height * yPercent / 100;

        for (int i = 0; i < getChildCount(); ++i) {
            final View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                final int childWidth = child.getMeasuredWidth();
                final int childHeight = child.getMeasuredHeight();
                // Center child both vertically and horizontally.
                final int childLeft = subLeft + (subWidth - childWidth) / 2;
                final int childTop = subTop + (subHeight - childHeight) / 2;
                child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
//        mBorderRect.left = childLeft-100;
//        mBorderRect.top  = childTop-100;
//        mBorderRect.bottom = childHeight+100;
//        mBorderRect.right  = childWidth+100;
            }
        }
    }

}

任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:1)

如果将RelativeLayout放在MyViewGroup布局中,该怎么办?

<com.testlayout.MyViewGroup
    android:id="@+id/parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </RelativeLayout>

</com.testlayout.MyViewGroup>

答案 1 :(得分:-1)

您只需要将引力设置为 viewgroup

RT