2017-06-06 34 views
0

RelativeLayout中,我有2個按鈕:buttonbutton2。這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駐留在內部佈局和添加到根後應該有相同的邊界和參數,屏幕上的位置也不能改變。

+0

我沒有得到它,爲什麼你希望你的按鈕,將RelativeLayout的移出,如果你不希望UI的改變? – Eselfar

+0

那是什麼目的?澄清用例。 – azizbekian

+0

@Eselfar - 我想要做的就是在佈局上方覆蓋目標'View',以便視圖的位置看起來不像它已經改變。之後,我將執行一個縮放動畫,將這個視圖繪製在其他視圖的上方。我試圖用'clipChildren'來做同樣的事情,但這並沒有成功。 –

回答

1

我認爲你的問題可能與button2的佈局參數有關。可能最簡單的方法是在佈局中捕獲button2的位置並設置新的佈局參數。 mLeftmTop爲左邊界和上邊界。

簡化的onCreate方法在下面,但我很好奇爲什麼這會有用。

另請參閱these caveatsView的文檔說明了爲什麼setTop等不應該在佈局系統之外被調用。 (參見here。)

頂置

空隙機頂盒(INT頂部)

設置該視圖相對於其父的頂部位置。這個方法意味着被佈局系統調用,通常不應該被另外調用,因爲這個屬性可能隨時被佈局改變。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    Button button; 

    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); 
    otherLayout = (RelativeLayout) findViewById(R.id.otherLayout); 

    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      RelativeLayout.LayoutParams params; 
      int top = button2.getTop(); 
      int left = button2.getLeft(); 

      ((ViewGroup) button2.getParent()).removeView(button2); 
      params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 
        ViewGroup.LayoutParams.WRAP_CONTENT); 
      params.setMargins(left, top, 0, 0); 
      button2.setLayoutParams(params); 
      rootView.addView(button2); 
     } 
    }); 
} 
相關問題