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>