片段更改后,Android FloatingActionButton没有消失

时间:2016-01-15 11:08:25

标签: android

我正在制作一个基本的Android应用程序。我在第一个片段中使用了一个按钮来导航到第二个片段。 最初,第一个片段并没有消失,而是与新片段重叠。然后我在片段中添加了一个背景,但按钮仍未消失。该按钮仍然保留在上一个片段中。 如何删除此按钮以便可以使用新片段的按钮?

HomeFragment.java

public class HomeFragment extends Fragment implements OnClickListener{
FloatingActionButton fab;

public HomeFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_home, container, false);
        ((FloatingActionButton) rootView.findViewById(R.id.fab)).setOnClickListener(this);
        return rootView;
    }

    public void onClick(View view) {
        TeamFragment fragment2 = new TeamFragment();
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction Transaction = fragmentManager.beginTransaction();
        Transaction.replace(R.id.fragmenthome, fragment2);
        Transaction.addToBackStack(null);
        Transaction.commit();
    }

TeamFragment.java

public class TeamFragment extends Fragment {

public TeamFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_teamname, container, false);
    // Inflate the layout for this fragment
    return rootView;
}

Fragment_home.xml

<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:orientation="vertical"
android:id="@+id/fragmenthome"
tools:context="app.tuhinsidd.cop290registration.activity.HomeFragment"
android:background="@android:color/white"
android:clickable="true">


<TextView
    android:id="@+id/label"
    android:layout_alignParentTop="true"
    android:layout_marginTop="80dp"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:textSize="45dp"
    android:text="WELCOME"
    android:textStyle="bold"/>

<TextView
    android:layout_below="@id/label"
    android:layout_centerInParent="true"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="12dp"
    android:layout_marginTop="20dp"
    android:gravity="center_horizontal"
    android:text="This is an app to register yourselves on the course COP290" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="40dp"
    android:elevation="3dp"
    android:tint="#ffffff"
    android:src="@android:drawable/ic_input_add"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />
<TextView
    android:layout_alignParentBottom="true"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="12dp"

    android:layout_marginBottom="10dp"
    android:gravity="center_horizontal"
    android:text="Created by Tuhinanksu Das and Siddharth Jain" />

</RelativeLayout>

fragment_teamname.xml

<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:orientation="vertical"
android:id="@+id/teamfragment"
tools:context="app.tuhinsidd.cop290registration.activity.FriendsFragment"
android:background="@android:color/white"
android:clickable="true">


<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="350dp"
    android:layout_marginTop="?attr/actionBarSize"
    android:orientation="vertical"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingTop="20dp">
<TextView
    android:id="@+id/label"
    android:layout_alignParentTop="true"
    android:layout_marginTop="10sp"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:textSize="45dp"
    android:text="TEAM NAME"
    android:textStyle="bold"/>

<TextView
    android:layout_below="@id/label"
    android:layout_centerInParent="true"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="12dp"
    android:layout_marginTop="20dp"
    android:gravity="center_horizontal"
    android:text="Please enter the name of your team below"
    android:id="@+id/textView" />


<android.support.design.widget.TextInputLayout
    android:id="@+id/input_layout_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/input_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:hint="@string/hint_name" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>




<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="40dp"
    android:elevation="3dp"
    android:tint="#ffffff"
    android:src="@drawable/ic_arrow"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />



</RelativeLayout>

如果需要其他任何内容,请告诉我。

1 个答案:

答案 0 :(得分:2)

提供FloatingActionButton

app:elevation="0dp"

原因: https://stackoverflow.com/a/34793158/4409113