【问题标题】:How to shift a LinearLayout when a SlidingDrawer expands?当 SlidingDrawer 展开时如何移动 LinearLayout?
【发布时间】:2016-02-29 13:53:20
【问题描述】:

我正在尝试在片段中实现一个面板,该面板可以在从屏幕右侧单击按钮时展开。我不能使用抽屉式导航,因为我已经有左右两侧的抽屉式导航了。

我们的想法是实现这样的目标:

我几乎可以使用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>

【问题讨论】:

  • 你的 LinearLayout 必须向左然后在另一个比他更近时变小?就像在展开的面板和左限制之间压缩一样
  • @Tritri 是的,没错。我试图改变LinearLayout 的严重性,但没有成功。目前,当我尝试获取LinearLayout 并更改参数时,我得到了java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams 异常..
  • 我认为这个错误是因为您的 LinearLayout 依赖于 RelativeLayout 所以:RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT); 您设置的重力是针对 textView 而不是 linearLayout 本身,但是如果您在 LinearLayout 中,layout_gravity 会起作用.但即使这样,如果它可以工作,我也不知道。我会用代码做更多的事情。

标签: android android-layout android-linearlayout slidingdrawer


【解决方案1】:

你可以像这样编辑你的 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,因此它将线性布局向左移动。希望对您有所帮助!

【讨论】:

  • 我不确定我理解在哪个线性布局的右侧设置什么。你能澄清一下吗?
  • 请用 cmets 检查编辑后的代码,如果有任何疑问,请告诉我。
  • 感谢 cmets。我已经尝试过了,但它不会改变左线性布局。即使在扩展滑动抽屉时它也保持静态,因为在应用启动时宽度是固定的。
  • 你有试过让滑动抽屉的宽度来包裹内容吗?
  • 是的,我试过了,但是它占用了整个屏幕。顺便说一句,我正在以横向模式在平板电脑上进行测试,但这没关系。
【解决方案2】:

如果你想要一个小例子,这里是我的解决方案:(我不知道 Avani Nagar 的解决方案是否有效):

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>

【讨论】:

    【解决方案3】:

    我设法做到了,实际上并不难,但有点棘手。这是解决方案:

    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 能够在抽屉打开时移动它的位置,并在抽屉关闭时回到原来的位置。下一步是用动画制作这个过程,所以它很流畅,不会只是来回跳到新位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多