在RelativeLayout
中,我有2個按鈕:button
和button2
。這3個本身位於根視圖中,這也是RelativeLayout
。將子視圖添加到具有相同邊界的父佈局
我想在這裏實現的是,當button
點擊button2
要從中刪除內RelativeLayout
(它的ID是otherLayout
),並會添加到根View
這是rootView
但具有相同的範圍,因爲它是在內部佈局。
這是我的XML和Java代碼:
<?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"
android:id="@+id/rootView"
tools:context="com.test.testproject.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/otherLayout"
>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="53dp"
android:layout_marginTop="94dp"
android:text="One" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button"
android:layout_marginStart="76dp"
android:layout_toEndOf="@+id/button"
android:text="Two" />
</RelativeLayout>
</RelativeLayout>
Java代碼的
public class MainActivity extends AppCompatActivity {
Button button,button2;
int mLeft,mRight,mTop,mBottom;
RelativeLayout otherLayout;
RelativeLayout rootView;
ViewGroup childParent;
int[] mBounds = new int[2];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
// rootView = (RelativeLayout) findViewById(R.id.rootView);
rootView = (RelativeLayout) findViewById(R.id.rootView);
otherLayout = (RelativeLayout) findViewById(R.id.otherLayout);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Rect r = new Rect();
button2.getLocalVisibleRect(r);
// button2.getDrawingRect(r);
// ((ViewGroup)button2.getParent()).offsetDescendantRectToMyCoords(button2, r);
mLeft = r.left;
mRight = r.right;
mBottom = r.bottom;
mTop = r.top;
childParent = (ViewGroup) button2.getParent();
((ViewGroup)button2.getParent()).removeView(button2);
Handler mHandler = new Handler(Looper.getMainLooper());
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
button2.setTop(mTop);
button2.setLeft(mLeft);
button2.setBottom(mBottom);
button2.setRight(mRight);
/*
button2.setX(mBounds[1]);
button2.setY(mBounds[0]);*/
((ViewGroup)rootView).addView(button2);
}
},1000);
}
});
}
}
我嘗試了不同的方法,但會努力。我使用getLocalVisibleRect()
,getDrawingRect()
和所有(您可以在註釋的代碼中看到)。
那麼我該如何實現呢?請記住,要求是View
駐留在內部佈局和添加到根後應該有相同的邊界和參數,屏幕上的位置也不能改變。
我沒有得到它,爲什麼你希望你的按鈕,將RelativeLayout的移出,如果你不希望UI的改變? – Eselfar
那是什麼目的?澄清用例。 – azizbekian
@Eselfar - 我想要做的就是在佈局上方覆蓋目標'View',以便視圖的位置看起來不像它已經改變。之後,我將執行一個縮放動畫,將這個視圖繪製在其他視圖的上方。我試圖用'clipChildren'來做同樣的事情,但這並沒有成功。 –