【问题标题】:Calling a LinearLayout from a FrameLayout从 FrameLayout 调用 LinearLayout
【发布时间】:2015-09-29 16:48:22
【问题描述】:

我在 FrameLayout 中加载了一个片段,并且片段中有一个按钮。单击按钮时,我想开始一个新的活动。但是当我启动 Activity 时,按钮仍然存在于新的 Activity 中。如何防止这种情况发生?

MainActivity.Java

<LinearLayout 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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:id="@+id/main">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:layout_scrollFlags="scroll|enterAlways"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:id="@+id/drawerLayout">


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/frame"/>



    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/drawer"
        android:id="@+id/navigation"/>
</android.support.v4.widget.DrawerLayout>

MainActivity2

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frame"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Test"/>

创建片段

public void onViewCreated(View view, Bundle savedInstanceState){
    super.onViewCreated(view, savedInstanceState);

    final Button button = (Button) view.findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getActivity(),Main2Activity.class);
            startActivity(intent);

        }
    });
}

Fragment.xml

<FrameLayout 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.anonymous.kalanjali2015.HomeFragment">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="next"
    android:id="@+id/button" />

【问题讨论】:

  • 你应该添加你的代码...
  • 请添加您的代码或提供一些链接。

标签: android android-fragments layout android-framelayout


【解决方案1】:

发生这种情况是因为您的MainActivity2 布局是透明的,并且在您的MainActivity 下方的堆栈中,您的片段位于,因此该片段的按钮在您的MainActivity2 中可见。

做一件事:在MainActivity2.xml 的LinearLayout 中添加一些不可见的深色背景颜色android:background="#ff0000"

【讨论】:

    猜你喜欢
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-28
    • 2017-07-28
    相关资源
    最近更新 更多