Android中相互重叠的功能

Android中相互重叠的功能,android,tabs,overlap,overlapping,Android,Tabs,Overlap,Overlapping,我正在构建一个Android应用程序,在我的主要活动中,我有一个“切换”按钮,它的正下方有一个带有三个不同选项卡的栏 开关按钮可设置为“开”和“关”。这是我尝试做的第一件事,效果很好。但在我添加了这个带有标签的条后,开关按钮停止工作,它似乎在这个条内有一些东西不允许开关工作。我是Android开发新手 这是我的主要活动代码: 如果我删除或注释“//Tabs”下面的所有内容,开关将再次开始工作,否则它不会从打开变为关闭 public class MainScreen extends AppCom

我正在构建一个Android应用程序,在我的主要活动中,我有一个“切换”按钮,它的正下方有一个带有三个不同选项卡的栏

开关按钮可设置为“开”和“关”。这是我尝试做的第一件事,效果很好。但在我添加了这个带有标签的条后,开关按钮停止工作,它似乎在这个条内有一些东西不允许开关工作。我是Android开发新手

这是我的主要活动代码:

如果我删除或注释“//Tabs”下面的所有内容,开关将再次开始工作,否则它不会从打开变为关闭

public class MainScreen  extends AppCompatActivity{

    Switch list_toggle;


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

        final TextView textView4 = (TextView) findViewById(R.id.textView4);

        textView4.setText(Html.fromHtml("Absent"));

        list_toggle = (Switch) findViewById(R.id.mySwitch);
        list_toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {

                    textView4.setText(Html.fromHtml("Available"));

                } else {

                    textView4.setText(Html.fromHtml("Absent"));

                }
            }
        });

        //Tabs

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.addTab(tabLayout.newTab().setText("Orders"));
        tabLayout.addTab(tabLayout.newTab().setText("Past Orders"));
        tabLayout.addTab(tabLayout.newTab().setText("More"));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        final PagerAdapter adapter = new PagerAdapter
                (getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
以下是我的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.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="@drawable/topbg"
        android:minHeight="?attr/actionBarSize">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:src="@drawable/top_logo" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Hi, Marcos"
                android:id="@+id/textView3"
                android:layout_alignParentTop="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:textSize="12dp" />

        </RelativeLayout>
    </android.support.v7.widget.Toolbar>

    <Switch
        android:id="@+id/mySwitch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:showText="true"
        android:textOn="ON"
        android:thumb="@drawable/customswitchselector"
        android:track="@drawable/custom_track"
        android:textOff="OFF"
        android:layout_marginTop="28dp"
        android:textColor="#ffffff"
        android:layout_marginLeft="50dp"
        android:layout_marginStart="50dp"
        android:layout_below="@+id/toolbar"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/textView4"
        android:layout_marginRight="69dp"
        android:layout_marginEnd="69dp"
        android:textColor="#a6a6a6"
        android:textSize="30dp"
        android:layout_alignBottom="@+id/mySwitch"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v4.view.ViewPager>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:elevation="6dp"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="149dp" />

        <android.support.v4.view.ViewPager
            android:id="@+id/pager1"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_below="@id/tab_layout"/>

    </RelativeLayout>

</RelativeLayout>

使用
RelativeLayout
意味着您可以相对地定位其子视图。例如,您可能会说希望
表格布局
显示在
开关
的下方。为了指示两个视图之间的关系,使用了以下两个属性:
android:layout\u低于
android:layout\u toRightOf
。您可以说我希望此视图位于具有以下资源ID的视图的下方或右侧。在您的情况下,
TabLayout
的布局应该是这样的:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/mySwitch">

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:elevation="6dp"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="149dp" />

    <android.support.v4.view.ViewPager
        android:id="@+id/pager1"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/tab_layout"/>

</RelativeLayout>

请注意,您已经在
开关
工具栏
之间建立了相同的关系

还有其他标记使用RelativeLayout(使视图居中或将其捕捉到布局边缘)。如果未为子视图提供任何这些选项,则默认情况下将在RelativeLayout的左上边缘绘制。如果那里已经有一个视图,那么它们将彼此重叠。如果没有为任何子视图提供定位信息,所有子视图都将在左上角绘制并捕捉,并且可以全部重叠。有时,这是我们正在寻找的行为,但在您的案例中并非如此。您没有在xml和Android中为选项卡部分的布局添加属性,只是将其绘制在开关的顶部。所以这个布局得到了
开关的触摸事件,这就是为什么它不工作的原因


但是为什么最后一个孩子的
开关起作用呢?因为视图是按照它们在xml中的顺序绘制的。因此,当
开关
是最后一个开关时,它是在RelativeLayout子级之后最后绘制的,并且它是位于顶部的开关。这意味着它现在可以获得触摸事件。但是,您可以看到如何使用属性是更好的解决方案。它们实际上是为了解决像您这样的情况,并且使用子视图的顺序有点微妙。

使用
RelativeLayout
意味着您可以相对地定位其子视图。例如,您可能会说希望
表格布局
显示在
开关
的下方。为了指示两个视图之间的关系,使用了以下两个属性:
android:layout\u低于
android:layout\u toRightOf
。您可以说我希望此视图位于具有以下资源ID的视图的下方或右侧。在您的情况下,
TabLayout
的布局应该是这样的:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/mySwitch">

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:elevation="6dp"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="149dp" />

    <android.support.v4.view.ViewPager
        android:id="@+id/pager1"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/tab_layout"/>

</RelativeLayout>

请注意,您已经在
开关
工具栏
之间建立了相同的关系

还有其他标记使用RelativeLayout(使视图居中或将其捕捉到布局边缘)。如果未为子视图提供任何这些选项,则默认情况下将在RelativeLayout的左上边缘绘制。如果那里已经有一个视图,那么它们将彼此重叠。如果没有为任何子视图提供定位信息,所有子视图都将在左上角绘制并捕捉,并且可以全部重叠。有时,这是我们正在寻找的行为,但在您的案例中并非如此。您没有在xml和Android中为选项卡部分的布局添加属性,只是将其绘制在开关的顶部。所以这个布局得到了
开关的触摸事件,这就是为什么它不工作的原因


但是为什么最后一个孩子的
开关起作用呢?因为视图是按照它们在xml中的顺序绘制的。因此,当
开关
是最后一个开关时,它是在RelativeLayout子级之后最后绘制的,并且它是位于顶部的开关。这意味着它现在可以获得触摸事件。但是,您可以看到如何使用属性是更好的解决方案。它们实际上是为了解决像您这样的情况,使用子对象的顺序有点微妙。

显示您的XML,我认为您的问题就在那里。我刚刚在帖子中添加了XML。似乎在RelativeLayout中定位的属性丢失了。如果这是一个问题,切割开关并将其粘贴到其相对位置的末尾(使其成为最后一个孩子)应该可以使其工作。你能试试吗?成功了!非常感谢你!但这不是一个好的解决方案。我会写一个答案并在那里解释。展示你的XML,我认为你的问题就在那里。我刚刚在帖子中添加了XML。在RelativeLayout中定位的属性似乎丢失了。如果这是一个问题,切割开关并将其粘贴到其相对位置的末尾(使其成为最后一个孩子)应该可以使其工作。你能试试吗?成功了!非常感谢你!但这不是一个好的解决方案。我会写一个答案并在那里解释。