Java 来自片段的Textview未显示在活动中

Java 来自片段的Textview未显示在活动中,java,android,xml,android-layout,android-fragments,Java,Android,Xml,Android Layout,Android Fragments,虽然我的程序运行时没有出现错误,但在emulator中运行程序时在fragment中创建的主_活动中没有显示文本视图。两者都使用约束布局活动,并且fragment和fragment没有显示在设计窗口的布局板中,因此我必须添加文本 爪哇 xml 尝试实现此--> 片段JAVA public class FragmentA extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater,

虽然我的程序运行时没有出现错误,但在emulator中运行程序时在fragment中创建的主_活动中没有显示文本视图。两者都使用约束布局活动,并且fragment和fragment没有显示在设计窗口的布局板中,因此我必须添加文本

爪哇

xml


尝试实现此-->

片段JAVA

public class FragmentA extends Fragment
{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_a,container,false);
}
片段XML(使用框架布局代替约束布局)

注意:如果使用constraintlayout,则会出现充气错误 主要活动:

<LinearLayout 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"

tools:context=".MainActivity">
<!-- display two Button's and a FrameLayout to replace the Fragment's  -->
   <Button
    android:id="@+id/firstFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="First Fragment"
    android:textSize="20sp" />


<FrameLayout
    android:id="@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="10dp" />
</LinearLayout>

您必须在MainActivity.xml文件上使用framlayout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

----Your Toolbar code here---------

    <FrameLayout
    android:id="@+id/framLayout"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

工作正常。

尝试设置文本颜色,然后再试一次……问题是,您不是在膨胀布局,而是在膨胀视图!您必须在此处充气布局视图v=充气机充气(R.layout.your_布局,容器,false);请添加布局截图,我的解决方案是否适用于您?
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout    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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="118dp"
    android:layout_marginBottom="132dp"
    android:layout_centerInParent="true"
    android:text="Hello World!"
   />
 </RelativeLayout>

 </FrameLayout>
public class MainActivity extends AppCompatActivity {

Button firstFragment;

@Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   // get the reference of Button's
    firstFragment = (Button) findViewById(R.id.firstFragment);

  // perform setOnClickListener event on First Button
    firstFragment.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
    // load First Fragment
            loadFragment(new YOURFRAGMENTNAME());
        }
    });
 //

}

    private void loadFragment(Fragment fragment) {
 // create a FragmentManager
    FragmentManager fm = getSupportFragmentManager();
   // create a FragmentTransaction to begin the transaction and replace the Fragment
    FragmentTransaction fragmentTransaction = fm.beginTransaction();
   // replace the FrameLayout with new Fragment
    fragmentTransaction.replace(R.id.frameLayout, fragment);
    fragmentTransaction.commit(); // save the changes
}
}
<LinearLayout 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"

tools:context=".MainActivity">
<!-- display two Button's and a FrameLayout to replace the Fragment's  -->
   <Button
    android:id="@+id/firstFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="First Fragment"
    android:textSize="20sp" />


<FrameLayout
    android:id="@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="10dp" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

----Your Toolbar code here---------

    <FrameLayout
    android:id="@+id/framLayout"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
private void addFragment(Fragment fragment) {
 // create a FragmentManager
    FragmentManager fm = getSupportFragmentManager();
   // create a FragmentTransaction to begin the transaction and replace the Fragment
    FragmentTransaction fragmentTransaction = fm.beginTransaction();
   // replace the FrameLayout with new Fragment
    fragmentTransaction.replace(R.id.frameLayout, fragment);
    fragmentTransaction.commit(); // save the changes
}