0

我試圖在一個片段中實現一個面板,該面板可以在屏幕右側點擊按鈕時展開。我無法使用導航抽屜,因爲我已經從左側和右側都有導航抽屜。如何在SlidingDrawer展開時移動LinearLayout?

的想法是要實現這樣的事情:

enter image description here

我幾乎能與SlidingDrawer小工具來做到這一點(它的過時..),唯一的問題是,我不知道如何使LinearLayout出現在中間,然後在單擊按鈕來擴大SlidingDrawer時進行移位。

我有以下代碼:

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Button handle = (Button) findViewById(R.id.handle); 
     SlidingDrawer drawer = (SlidingDrawer) findViewById(R.id.slidingDrawer); 

     drawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() { 
      @Override 
      public void onDrawerOpened() { 
      } 
     }); 

     drawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() { 
      @Override 
      public void onDrawerClosed() { 
      } 
     }); 
    } 
} 

而且XML代碼:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.test.slidingdrawertest.slidingdrawertest.MainActivity"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:orientation="vertical"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="TEST TEST TEST" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_alignParentEnd="true" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent"> 


     <SlidingDrawer 
      android:id="@+id/slidingDrawer" 
      android:layout_width="200dp" 
      android:layout_height="400dp" 
      android:layout_marginTop="100dp" 
      android:layout_gravity="end" 
      android:content="@+id/content" 
      android:handle="@+id/handle" 
      android:orientation="horizontal"> 

      <Button 
       android:id="@+id/handle" 
       style="?android:attr/borderlessButtonStyle" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Expand" /> 

      <LinearLayout 
       android:id="@+id/content" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="TEST TEST TEST" /> 

      </LinearLayout> 

     </SlidingDrawer> 

    </LinearLayout> 

</RelativeLayout> 
+0

當另一個人比他更近時,您的LinearLayout必須向左移動然後變小?就像在擴展面板和左邊界限之間壓縮一樣 – king

+0

@Tritri是的,沒錯。我試圖改變'LinearLayout'的重力,但沒有成功。目前,我得到一個'java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams不能轉換爲android.widget.LinearLayout $ LayoutParams'異常,當我嘗試獲取'LinearLayout'並更改參數時.. – Apostrofix

+0

I認爲這個錯誤是因爲你的LinearLayout依賴於RelativeLayout,所以:'RelativeLayout。LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT);'你設置的重力是爲textView而不是linearLayout本身,但如果你在LinearLayout中,layout_gravity會起作用。但即使如此,如果它可以工作,我也不會。我會用代碼做更多的事情。 – king

回答

0

我設法做到這一點,實際上並不困難,但這有點棘手。這裏是解決方案:

XML代碼:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/topLayout" 
    tools:context="com.test.slidingdrawertest.slidingdrawertest.MainActivity"> 

    <LinearLayout 
     android:id="@+id/mainLayout" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:orientation="vertical"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="TEST" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_alignParentEnd="true" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent"> 

     <SlidingDrawer 
      android:id="@+id/slidingDrawer" 
      android:layout_width="200dp" 
      android:layout_height="400dp" 
      android:layout_marginTop="100dp" 
      android:layout_gravity="end" 
      android:content="@+id/content" 
      android:handle="@+id/handle" 
      android:orientation="horizontal"> 

      <Button 
       android:id="@+id/handle" 
       style="?android:attr/borderlessButtonStyle" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Expand" /> 

      <LinearLayout 
       android:id="@+id/content" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="TEST TEST TESTT" /> 

      </LinearLayout> 
     </SlidingDrawer> 
    </LinearLayout> 
</RelativeLayout> 

MainActivity.java代碼:

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Button handle = (Button) findViewById(R.id.handle); 
     SlidingDrawer drawer = (SlidingDrawer) findViewById(R.id.slidingDrawer); 
     final View k = findViewById(R.id.mainLayout); 

     drawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() { 
      @Override 
      public void onDrawerOpened() { 
       RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
         RelativeLayout.LayoutParams.WRAP_CONTENT); 
       params.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
       k.setLayoutParams(params); 
      } 
     }); 

     drawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() { 
      @Override 
      public void onDrawerClosed() { 
       RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
         RelativeLayout.LayoutParams.WRAP_CONTENT); 
       params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
       k.setLayoutParams(params); 
      } 
     }); 
    } 
} 

而且解釋:我不得不放棄主主要佈局的ID(XML格式)和然後在後面的代碼中找到它,然後在抽屜打開或關閉時必須應用新參數。基本上,我們需要找到父級佈局併爲其設置參數。

現在LinearLayout能夠將其在抽屜打開位置上移動,並且它在抽屜關閉時回到原來的位置。下一步是用動畫製作這個過程,所以它很流暢,不會來回跳到新的位置。

0

如果設置的任何佈局您可以編輯XML這樣

?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="10dp" 
    tools:context="com.test.slidingdrawertest.slidingdrawertest.MainActivity"> 

    <LinearLayout 
     android:id="@+id/left"    // this is your linear layout 
     android:layout_width="match_parent" // that you want to shift left 
     android:layout_height="match_parent" 
     android:layout_toLeftOf="@+id/right" // apply this property due to 
    android:layout_alignParentLeft="true" //which if the width of sliding menu 
     android:gravity="center"    //increase it will automatically compressed 
     android:background="#aa00aa" 


     android:orientation="vertical"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="TEST TEST TEST" /> 

    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/right" 
     android:layout_alignParentRight="true" 
     android:layout_width="wrap_content" 
     android:background="#0000aa" 
     android:layout_height="match_parent"> 


     <SlidingDrawer 
      android:id="@+id/slidingDrawer" 
      android:layout_width="200dp"   // you can check by increasing or decreasing the with of this slidingdrawer 
      android:layout_height="400dp" 
      android:layout_marginTop="100dp" 
      android:layout_gravity="end" 
      android:content="@+id/content" 
      android:handle="@+id/handle" 
      android:orientation="horizontal"> 

      <Button 
       android:id="@+id/handle" 
       style="?android:attr/borderlessButtonStyle" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Expand" /> 

      <LinearLayout 
       android:id="@+id/content" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="TEST TEST TEST" /> 

      </LinearLayout> 

     </SlidingDrawer> 

    </LinearLayout> 

</RelativeLayout> 
從這個

除了在線性佈局的右側,最初是GONE,然後單擊在按鈕上其可見性更改爲INVISIBLE/VISIBLE,因此它會將線性佈局移至左側。希望有所幫助!

+0

我不知道我明白要設置在哪個線性佈局的右側。你能澄清一下嗎? – Apostrofix

+0

請檢查編輯的代碼與評論,讓我知道如果有任何疑問。 –

+0

感謝您的評論。我已經嘗試過,但不會左移線性佈局。即使在展開滑動抽屜時它仍保持靜止,因爲寬度在應用程序啓動時是固定的。 – Apostrofix

0

如果你想在這裏一點點爲例是我的解決方案:(我不知道阿娃妮格爾的解決方案適用與否):

TextView res; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_main); 

    final TextView img = (TextView)findViewById(R.id.second); 
    res = (TextView)findViewById(R.id.first); 

    img.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      img.setX(img.getX() - 5); 
      changew(img.getX()); 
     } 
    }); 
} 

private void changew(float w){ 
    res.getLayoutParams().width = (int)(res.getX() +w); 
    res.requestLayout(); 
    res.invalidate(); 
} 

的XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@android:color/black"> 

<TextView 
    android:layout_toRightOf="@+id/first" 
    android:text="bonjourrrrrr" 
    android:id="@+id/second" 
    android:layout_width="wrap_content" 
    android:layout_height="100dp" 
    android:background="@android:color/holo_red_dark" /> 

<TextView 
    android:text="salutttttttt" 
    android:id="@id/first" 
    android:layout_width="wrap_content" 
    android:layout_height="100dp" 
    android:background="@android:color/holo_green_light" /> 


</RelativeLayout>