家长活动下的开放片段

时间:2018-07-19 07:49:45

标签: java android android-activity fragment

我想在活动中但在活动对象下打开一个片段。 在我的代码中RelativeLayout是父级,而LinearLayout是子级 但是当在relativelayout上打开片段时,我会丢失linearlayout :( 这是我的代码:

主要活动Java:

    public class MainActivity extends AppCompatActivity  implements View.OnClickListener{
private Context context;
private Button  fragment1 , fragment2;
private RelativeLayout relativeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context = getApplicationContext();

    fragment1 = this.findViewById(R.id.fragment1);
    fragment2 = this.findViewById(R.id.fragment2);
    relativeLayout = this.findViewById(R.id.relativeLayout);

    fragment1.setOnClickListener(this);
    fragment2.setOnClickListener(this);
  }


@Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.fragment1:
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.replace(R.id.relativeLayout , new fragment1());
            ft.commit();
            break;

        case R.id.fragment2:
            FragmentManager fm2 = getSupportFragmentManager();
            FragmentTransaction ft2 = fm2.beginTransaction();
            ft2.replace(R.id.relativeLayout , new fragment2());
            ft2.commit();
            break;
           }
    }
}

主要活动XML:

<?xml version="1.0" encoding="utf-8"?>
<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"
  tools:context=".MainActivity"
  android:id="@+id/relativeLayout">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:id="@+id/layout"
    android:background="#F00"
    android:orientation="horizontal">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="fragment 1"
        android:id="@+id/fragment1"
        android:gravity="center"
        android:layout_gravity="center"/>

    <Button
        android:id="@+id/fragment2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="fragment 2" />

</LinearLayout>

</RelativeLayout>

fragment1 XML:

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0F0">

 </android.support.constraint.ConstraintLayout>

fragment2 XML:

<?xml version="1.0" encoding="utf-8"?>
 <android.support.constraint.ConstraintLayout
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#00F">
 </android.support.constraint.ConstraintLayout> 

这是主要活动:enter image description here

,然后单击fragment1:enter image description here

请帮助我:)

TNX

3 个答案:

答案 0 :(得分:0)

在linearlayout之前的relativelayout中添加一个framelayout,并在framelayout中为片段充气。

 <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"
          tools:context=".MainActivity"
          android:id="@+id/relativeLayout">

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

        <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="60dp"
          android:id="@+id/linearLayout"
          android:background="#F00"
          android:orientation="horizontal">

       <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="fragment 1"
         android:id="@+id/fragment1"
         android:gravity="center"
         android:layout_gravity="center"/>

        <Button
          android:id="@+id/fragment2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:gravity="center"
          android:text="fragment 2" />

      </LinearLayout>

    </RelativeLayout>

答案 1 :(得分:0)

我认为您应该将Fragment1 XML和Fragment2 XML从Constraint Layout更改为LinearLayout。否则,应该可以在MainActivity中启动LineaLayout。

答案 2 :(得分:0)

您正在放大“父版式”上的片段。使Relativelayout的两个子节点像这样膨胀一个片段:

<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"
      tools:context=".MainActivity"
      android:id="@+id/relativeLayout">

    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="60dp"
      android:id="@+id/linearLayout"
      android:background="#F00"
      android:orientation="horizontal">

   <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="fragment 1"
     android:id="@+id/fragment1"
     android:gravity="center"
     android:layout_gravity="center"/>

    <Button
      android:id="@+id/fragment2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:gravity="center"
      android:text="fragment 2" />

  </LinearLayout>

<!-- inflate fragments on FrameLayout -->
  <FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/linearLayout">

  </FrameLayout>

</RelativeLayout>

在此FrameLayout上添加片段  ....希望这会有所帮助:)