内容更改后,布局应保持在特定位置

时间:2010-09-14 10:34:49

标签: android layout position

我有一个包含TextView的布局。 TextView的内容可能会在运行时更改。 (在这里讨论了另一个问题:Layout with dynamic position) 我可以通过拖放来定位布局。如果布局的大小没有改变,则位置保持不变,但是一旦布局的大小发生变化(因为TextView内的更多文本),位置就会重置。 我使用layout(left, top, right, bottom)来定位布局。

现在我不知道应该在哪里添加layout()电话,以确保它保持在正确的位置......

这是我的班级

public class TextNoteOverlay extends FrameLayout implements OverlayView {

    private TextView mNoteText;
    private LinearLayout mLinearLayout;
    public static int mX = 0;
    public static int mY = 0;
    public static int mStartX = 0;
    public static int mStartY = 0;
    public boolean mShow;
    private Rect mNoteRect = new Rect();

    public TextNoteOverlay(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        li.inflate(R.layout.textnote, this);
        mNoteText = (TextView) findViewById(R.id.textnote_content);
        mLinearLayout = (LinearLayout) findViewById(R.id.textnote_layout);

        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                mLinearLayout.getHitRect(mNoteRect);
                if (mNoteRect.contains((int) event.getX(), (int) event.getY())) {
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        mStartX = (int) event.getX() - mLinearLayout.getLeft();
                        mStartY = (int) event.getY() - mLinearLayout.getTop();
                        return true;
                    }
                    mX = (int) event.getX() - mStartX;
                    mY = (int) event.getY() - mStartY;
                    mLinearLayout.layout(mX, mY, mX + mLinearLayout.getWidth(), mY + mLinearLayout.getHeight());
                    return true;
                }
                return false;
            }
        });
    }

    @Override
    public void hide() {
        mShow = false;
        setVisibility(View.GONE);
    }

    @Override
    public boolean isVisible() {
        return mShow;
    }

    @Override
    public void setContent(Object content) {
        if (content != null && content instanceof Content) {
            Content noteContent = (Content) content;
            if (noteContent.getText() != null) {
                mNoteText.setText(noteContent.getText());
            } else {
                mNoteText.setText("");
            }
        }
        mNoteText.invalidate();
        mLinearLayout.invalidate();
    }

    @Override
    public void show() {
        if (mNoteText.getText().toString().length() > 0) {
            mShow = true;
            setVisibility(View.VISIBLE);
        }
    }
}

我的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/transparent">
    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/postit"
        android:id="@+id/textnote_layout">
        <TextView android:id="@+id/textnote_content"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:maxWidth="200dp"
            android:maxHeight="200dp"
            android:minWidth="100dp"
            android:minHeight="100dp"
            android:singleLine="false"
            android:layout_margin="5dp"
            android:textColor="#000000" />
    </LinearLayout>
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

我得到了工作,扩展了AbsoluteLayoutsetPadding()而不是layout()。我的代码:

@SuppressWarnings("deprecation")
public class TextNoteOverlay extends AbsoluteLayout implements OverlayView {

    private TextView mNoteText;
    private LinearLayout mLinearLayout;
    public static int mX = 0;
    public static int mY = 0;
    public static int mStartX = 0;
    public static int mStartY = 0;
    public boolean mShow;
    private Rect mNoteRect = new Rect();

    public TextNoteOverlay(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        li.inflate(R.layout.textnote, this);
        mNoteText = (TextView) findViewById(R.id.textnote_content);
        mLinearLayout = (LinearLayout) findViewById(R.id.textnote_layout);

        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                mLinearLayout.getHitRect(mNoteRect);
                mNoteRect.left += mX;
                mNoteRect.top += mY;
                mNoteRect.right = mNoteRect.left + mLinearLayout.getWidth();
                mNoteRect.bottom = mNoteRect.top + mLinearLayout.getHeight();
                if (mNoteRect.contains((int) event.getX(), (int) event.getY())) {
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        mStartX = (int) event.getX() - mNoteRect.left;
                        mStartY = (int) event.getY() - mNoteRect.top;
                        return true;
                    }
                    mX = (int) event.getX() - mStartX;
                    mY = (int) event.getY() - mStartY;

                    setPadding(mX, mY, 0, 0);
                    return true;
                }
                setPadding(mX, mY, 0, 0);
                return false;
            }
        });
    }

    // snipped
}