繁体   English   中英

家长活动下的开放片段

[英]open fragment under parent activity

我想在活动中但在活动对象下打开一个片段。 在我的代码中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> 

这是主要活动: 在此处输入图片描述

当单击fragment1时: 在此处输入图像描述

请帮我:)

TNX

在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>

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

您正在放大“父版式”上的片段。 使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上填充片段...希望有帮助:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM