来自与片段

时间:2016-10-26 10:12:26

标签: android android-fragments button

我有一个带有按钮和文本字段以及更多内容的活动。在该活动中,其中一个按钮在活动之上动态打开一个片段:

    btnGate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            sgFrag = new SetGateFragment();
            fragmentManager = getFragmentManager();
            transaction = fragmentManager.beginTransaction();
            transaction.add(R.id.container,sgFrag);
            transaction.addToBackStack(null);
            transaction.commit();
        }
    });

在这个片段中我也有几个按钮。现在,当我在手机上测试我的应用程序时,检查活动中的所有按钮以及片段中的按钮是否正常工作一切正常,我能够点击活动中的按钮以及片段,他们都相应地通信,但后来我决定在galaxy note5上测试我的应用程序,这次当我点击按钮打开片段时,活动中的一个按钮重叠片段并且可以点击而两个按钮都没有片段是可点击的。 不确定是什么原因,这是片段开头标记:

<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.example.phonegate.SetGateFragment"
android:background="@drawable/backgroundfrag"
android:orientation="vertical"
android:weightSum="1" />

这是我的活动布局开始标记:

<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="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.phonegate.MainActivity"
tools:showIn="@layout/activity_main"
android:background="@drawable/background"
android:id = "@+id/container"
android:orientation="vertical"> 

谢谢!

1 个答案:

答案 0 :(得分:0)

试试这段代码..!

FragmentManager fm = getSupportFragmentManager();
SetGateFragment fragment = new SetGateFragment();
fm.beginTransaction().add(R.id.main_contenier,fragment).commit();