Layout should stay at a specific position after content change
I have a layout which contains a TextView
. The content of the TextView
might change on runtime. (discussed on a different issue here: Layout with dynamic position )
I can position the layout by drag&drop. If the size of the layout doesn't change, the position stays, but as soon as the size of the layout changes (because more text inside the TextView
) the position is reseted.
I use the layout(left, top, right, bottom)
to position the layout.
Now I don't know where I should add my layout()
call to make sure it stays at the right position...
Here is my class
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);
}
}
}
My layout
<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/textno开发者_如何转开发te_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>
I got it working extending the AbsoluteLayout
and setPadding()
instead of layout()
. My code:
@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
}
精彩评论