動的位置のレイアウト
-
02-10-2019 - |
質問
少し珍しいかもしれないレイアウトがあります。構造は次のとおりです。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<org.CustomSurfaceView
android:id="@+id/page_flip"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="20dip" />
<org.customRelativeLayout
android:id="@+id/note"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone" />
</FrameLayout>
XMLの下部にある私のCustomRelativeLayoutには、XMLレイアウトもあります。
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="@drawable/my_background"
android:id="@+id/note_layout">
<TextView android:id="@+id/note"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:singleLine="false"
android:layout_margin="5dp"
android:textColor="#000000" />
</LinearLayout>
私のcustomrelativeLayout:
public class CustomRelativeLayout extends RelativeLayout implements OverlayView {
private TextView mNoteText;
private LinearLayout mLinearLayout;
public int mX = 0;
public int mY = 0;
public boolean mShow;
public CustomRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
li.inflate(R.layout.note, this);
mNoteText = (TextView) findViewById(R.id.note);
mLinearLayout = (LinearLayout) findViewById(R.id.note_layout);
mLinearLayout.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.e("touched");
mX = (int) event.getX() - 100;
mY = (int) event.getY() - 100;
invalidate();
return false;
}
});
}
@Override
public void hide() {
mShow = false;
setVisibility(View.GONE);
}
@Override
public boolean isVisible() {
return isVisible();
}
@Override
protected void onDraw(Canvas canvas) {
Log.e("Drawing the postit");
Matrix matrix = new Matrix();
matrix.postTranslate(mX, mY);
canvas.setMatrix(matrix);
super.onDraw(canvas);
}
@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() {
mShow = true;
setVisibility(View.VISIBLE);
}
}
私がやりたいこと:CustomRelativeLayoutの位置を変更できるようになりたいです。それは私のタッチに従うべきです。 SurfaceViewよりも小さく、私の触れた後、上に「スライド」する必要があります...
2つの問題があります。
- ontouchListenerは、mlinearlayout(customrelativeLayout)にバインドされます。Action_Downのみがトリガーされます。ログ出力は一度だけ表示されます
- メモは位置を変えることはありません。変更されたマトリックスで再描画されることはありません...
onDraw
触れてから決して起こりませんでした。
1つ目は、SurfaceViewがタッチイベントも処理するためかもしれません。しかし、CustomRelativeLayoutが最初にそれを処理する場合、それは機能するはずだと思いました。
いくつかのアイデア?
ソリューションの編集
Xil3のおかげで、私は遭遇した壁を取り除くことができました...ここに私の解決策があります:
私のメモXML:
<?xml version="1.0" encoding="UTF-8"?>
<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="200dp"
android:layout_height="200dp"
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:singleLine="false"
android:layout_margin="5dp"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
そして、これが私のコンストラクターです:
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;
}
});
}
また、バグ/機能/問題を見つけました。これは私にとってまったく新しいものです。ノートルート要素(現在透明です)の背景を削除すると、メモは内側に設定した200dp幅/高さの範囲内でのみ表示されます。 LinearLayout。だからそうではありません fill_parent
, 、 これは wrap_content
私がそれを設定したとしても fill_parent
. 。それで layout_width
と layout_height
与えられたもののみを使用してください fill_parent
背景が設定されているとき...除草...
解決
問題は、Ontouchイベントで虚偽を返していることです。したがって、基本的には、その後のイベントに興味がないことを基本的に伝えています。
それを変更してtrueを返します。