【问题标题】:SlidingDrawer underlap in RelativeLayout相对布局中的 SlidingDrawer 重叠
【发布时间】:2017-07-26 14:31:58
【问题描述】:

我在 tutorial 中使用了 SlidingDrawer

它在 LinearLayout 中工作,但根据滑块的高度,总会有一个空白,所有其他内容都会显示在空白下方。

解决方案应该是使用相对布局。这会删除空格,但滑块中的处理程序按钮似乎位于滚动视图中的内容下方,并且通过单击它不执行任何操作。看截图

MainActivity.xml

<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="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<include
    android:id="@+id/tbar"
    layout="@layout/primerdivisor" />

<SlidingDrawer
    android:id="@+id/SlidingDrawer"
    android:layout_width="wrap_content"
    android:layout_height="150dp"
    android:content="@+id/contentLayout"
    android:handle="@+id/slideButton"
    android:orientation="vertical"
    android:padding="10dip"
    android:rotation="180">
    <!-- Handle button -->


    <Button
        android:id="@+id/slideButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button_selector"
        android:ems="10"
        android:rotation="180"
        android:text="Show" android:clickable="true"/>

    <!-- Content Layout -->
    <LinearLayout
        android:id="@+id/contentLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FFCC"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="10dip">

        <Button
            android:id="@+id/Button01"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:background="@drawable/button_selector"
            android:text="Button 1"
            android:textColor="@color/textBlack" />

        <TextView
            android:id="@+id/text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:background="@drawable/button_selector"
            android:gravity="center"
            android:padding="5dp"
            android:text="Text View Item"
            android:textColor="@color/textBlack" />
    </LinearLayout>
</SlidingDrawer>

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        android:orientation="vertical">
....

MainActivity.java

...
private void createSlider(){
    slideButton = (Button) findViewById(R.id.slideButton);
    slidingDrawer = (SlidingDrawer) findViewById(R.id.SlidingDrawer);
    b1 = (Button) findViewById(R.id.Button01);
    textView = (TextView) findViewById(R.id.text);

    // Setting Listeners to all buttons and textview
    setListeners();

    // Listeners for sliding drawer
    slidingDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {
        @Override
        public void onDrawerOpened() {

            // Change button text when slider is open
            Log.i("----","blaaaaaa open");
            slideButton.setText("Close");
        }
    });

    slidingDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener() {
        @Override
        public void onDrawerClosed() {

            // Change button text when slider is close
            slideButton.setText("Open");
        }
    });
}

// Listeners method
void setListeners() {
    b1.setOnClickListener(this);
    textView.setOnClickListener(this);
}

@Override
public void onClick(View v) {

    // Toast shown on sliding drawer items click
    if (v.getId() == R.id.text) {
        Toast.makeText(MainActivity.this, textView.getText() + " Clicked",
                Toast.LENGTH_SHORT).show();
    } else {
        Button b = (Button) v;
        Toast.makeText(MainActivity.this, b.getText() + " Clicked",
                Toast.LENGTH_SHORT).show();
    }
}
...

滑出时滑块应与滚动视图重叠

【问题讨论】:

  • 将您的SlidingDrawer 移至其他内容下方
  • 我没想到。谢谢它的工作原理
  • 我已经发布了一个答案,因为这条评论解决了这个问题。

标签: android android-linearlayout android-relativelayout slidingdrawer


【解决方案1】:

在视图层次结构术语中,在布局开头XML 定义的视图比后面定义的视图“更深”。

在您的情况下,您的 ScrollView 被添加到您的 SlidingDrawer 之后的层次结构中,所以您看到的结果是可以预料的。

将您的SlidingDrawer 移动到您的ScrollView 下方,它应该在它上方。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多