java - fragment 已添加到 Activity 但不显示

标签 java android android-fragments android-activity

我创建了一个 Activity 并尝试将 fragment 插入到该 Activity 中,但是运行时该 fragment 在创建 Activity 时不会出现。只是一个空格,没有构建错误。

感谢您提前提供的任何帮助。

MainActivity.java

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final HomeFragment homeFragment = new HomeFragment();

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        getFragmentManager().beginTransaction()
                .replace(R.id.main_container, homeFragment).commit();
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_activity_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        } else if (id == R.id.search_mag_icon){
            // Open intent here
            Intent i = new Intent(getApplicationContext(), SearchActivity.class);
            startActivity(i);
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_register) {
            Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
            startActivity(i);
        } else if (id == R.id.nav_login) {
            Intent i = new Intent(getApplicationContext(), LoginActivity.class);
            startActivity(i);
        } else if (id == R.id.nav_my_listings) {
            // navigate to my listings
        } else if (id == R.id.nav_how) {
            // navigate to how it works page
        } else if (id == R.id.nav_help) {
            // navigate to help page
        } else if (id == R.id.nav_contact_us) {
            // navigate to contact page,
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

HomeFragment.java

public class HomeFragment extends Fragment {

    ImageView bookIcon;
    ImageView barcodeIcon;
    ImageView messageIcon;
    ImageView listIcon;


    public HomeFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_home, container, false);

        setHomeIcons(rootView);

        return rootView;
    }

    public void setHomeIcons(View rootView){
        bookIcon = (ImageView)rootView.findViewById(R.id.bookIcon);
        bookIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Launching All products Activity
                Intent i = new Intent(getActivity().getApplicationContext(), BrowseUniCourseActivity.class);
                startActivity(i);
            }
        });

        barcodeIcon = (ImageView)rootView.findViewById(R.id.barcodeIcon);
        barcodeIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar.make(v, "Will open 'Barcode' Intent", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        messageIcon = (ImageView)rootView.findViewById(R.id.messageIcon);
        messageIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar.make(v, "Will open 'My Messages' Intent", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();

            }
        });

        listIcon = (ImageView)rootView.findViewById(R.id.listIcon);
        listIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar.make(v, "Will open 'My Listings' Intent", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    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:id="@+id/drawer_layout"
    android:background="#FFF"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <include
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            layout="@layout/app_bar_main" />

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

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        android:background="@color/colorPrimaryDark"
        app:itemTextColor="#FFF"
        app:itemIconTint="#FFF"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- Book icon and text -->
        <ImageView
            android:id="@+id/bookIcon"
            android:layout_width="100dip"
            android:layout_height="100dip"
            android:layout_marginLeft="50dip"
            android:layout_marginTop="110dip"
            android:src="@drawable/book" />

        <TextView
            android:id="@+id/bookLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/bookIcon"
            android:layout_marginLeft="20dip"
            android:text="@string/bookLabel"
            android:textColor="@color/colorAlt"
            android:textStyle="bold" />


        <!-- Barcode icon and text -->
        <ImageView
            android:id="@+id/barcodeIcon"
            android:layout_width="95dip"
            android:layout_height="95dip"
            android:layout_marginLeft="260dip"
            android:layout_marginTop="120dip"
            android:src="@drawable/barcode" />

        <TextView
            android:id="@+id/barcodeLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/barcodeIcon"
            android:layout_marginLeft="260dip"
            android:paddingTop="5dip"
            android:text="@string/barcodeLabel"
            android:textColor="@color/colorAlt"
            android:textStyle="bold" />


        <!-- Message icon and text -->
        <ImageView
            android:id="@+id/messageIcon"
            android:layout_width="100dip"
            android:layout_height="100dip"
            android:layout_marginLeft="50dip"
            android:layout_marginTop="350dip"
            android:src="@drawable/email" />

        <TextView
            android:id="@+id/messageLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/messageIcon"
            android:layout_marginLeft="55dip"
            android:text="@string/messageLabel"
            android:textColor="@color/colorAlt"
            android:textStyle="bold" />

        <!-- List icon and text -->
        <ImageView
            android:id="@+id/listIcon"
            android:layout_width="95dip"
            android:layout_height="95dip"
            android:layout_marginLeft="260dip"
            android:layout_marginTop="350dip"
            android:src="@drawable/list" />

        <TextView
            android:id="@+id/listLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/listIcon"
            android:layout_marginLeft="273dip"
            android:paddingTop="5dip"
            android:text="@string/listLabel"
            android:textColor="@color/colorAlt"
            android:textStyle="bold" />

    </RelativeLayout>

最佳答案

好的,我想我明白了。将包含移动到 LinearLayout 的外部

<include
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        layout="@layout/app_bar_main" />


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <FrameLayout
        android:id="@+id/main_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>
</LinearLayout>

关于java - fragment 已添加到 Activity 但不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34571336/

相关文章:

java - 如何在android中获取所有字体目录字体名称?

java - 移动数据时的空指针

java - 安卓。以编程方式添加控件

java - 使数据模型类可序列化

java - Android 客户端无法打开到 C 服务器的套接字

android - 在后台创建新 Intent

android - 如何获取作为属性引用的维度值?

android - ListFragment 和菜单选项

android - 在哪里恢复 ViewPager 内部的 fragment 状态

java - neo4j 直接访问和通过 OGM 之间的显着性能差异