繁体   English   中英

片段中的文本视图未显示在活动中

[英]Textview from fragment not showing up in activity

设计窗口截图 虽然我的程序运行没有错误,但它没有在我在模拟器中运行程序时在片段中创建的 main_activity 中显示文本视图。两者都使用约束布局活动以及片段和片段没有显示在布局中Platte 在设计窗口中,所以我不得不添加文本。

爪哇

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class FragmentA extends Fragment
{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_a,container,false);
        return v;
    }

xml

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

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="118dp"
        android:layout_marginBottom="132dp"
        android:text="Hello World!"
        app:layout_constraintBottom_toTopOf="@+id/fragment_a"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <fragment
        android:id="@+id/fragment_a"
        android:name="com.shivam.fragmentsa1.FragmentA"
        android:layout_width="175dp"
        android:layout_height="50dp"
        android:layout_marginBottom="384dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.538"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

</androidx.constraintlayout.widget.ConstraintLayout>

尝试实现这个-->

片段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 (而不是使用约束布局使用Framelayout

注意:如果你使用constraintlayout你会得到膨胀错误

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

您必须在 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>

您的 MainActivity.java 类仅添加此方法并仅传递您的片段对象。

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
}

它工作正常。

暂无
暂无

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

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