片段加入到活性,但不显示 [英] Fragment added to activity but does not show

查看:249
本文介绍了片段加入到活性,但不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个活动,并试图插入片段导入的活性,但在运行时创建的活动时的片段不会出现。只是一个空白,没有生成错误。

感谢提前任何帮助。

MainActivity.java

 公共类MainActivity扩展AppCompatActivity实现NavigationView.OnNavigationItemSelectedListener {
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        最后HomeFragment homeFragment =新HomeFragment();        工具条工具栏=(栏)findViewById(R.id.toolbar);
        setSupportActionBar(工具栏);        DrawerLayout抽屉=(DrawerLayout)findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle切换=新ActionBarDrawerToggle(
                对此,抽屉,工具栏,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
        drawer.setDrawerListener(切换);
        toggle.syncState();        NavigationView navigationView =(NavigationView)findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(本);        getFragmentManager()调用BeginTransaction()
                .replace(R.id.main_container,homeFragment).commit();
    }    @覆盖
    公共无效onBack pressed(){
        DrawerLayout抽屉=(DrawerLayout)findViewById(R.id.drawer_layout);
        如果(drawer.isDrawerOpen(GravityCompat.START)){
            drawer.closeDrawer(GravityCompat.START);
        }其他{
            super.onBack pressed();
        }
    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.main_activity_main,菜单);
        返回true;
    }    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        //处理动作栏项目点击这里。操作栏会
        //自动处理上点击主页/向上按钮,只要
        //你在AndroidManifest.xml中指定一个父活动。
        INT ID = item.getItemId();        // noinspection SimplifiableIfStatement
        如果(ID == R.id.action_settings){
            返回true;
        }否则如果(ID == R.id.search_mag_icon){
            //此处打开意图
            意图I =新意图(getApplicationContext(),SearchActivity.class);
            startActivity(ⅰ);
        }        返回super.onOptionsItemSelected(项目);
    }    @燮pressWarnings(StatementWithEmptyBody)
    @覆盖
    公共布尔onNavigationItemSelected(菜单项项){
        //处理导航视图项点击这里。
        INT ID = item.getItemId();        如果(ID == R.id.nav_register){
            意图I =新意图(getApplicationContext(),RegisterActivity.class);
            startActivity(ⅰ);
        }否则如果(ID == R.id.nav_login){
            意图I =新意图(getApplicationContext(),LoginActivity.class);
            startActivity(ⅰ);
        }否则如果(ID == R.id.nav_my_listings){
            //浏览我的房源
        }否则如果(ID == R.id.nav_how){
            //浏览到它是如何工作页面
        }否则如果(ID == R.id.nav_help){
            //导航帮助页面
        }否则如果(ID == R.id.nav_contact_us){
            //浏览网页联系,
        }        DrawerLayout抽屉=(DrawerLayout)findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        返回true;
    }
}

HomeFragment.java

 公共类HomeFragment扩展片段{    ImageView的bookIcon;
    ImageView的酒吧codeIcon;
    ImageView的messageIcon;
    ImageView的listIcon;
    公共HomeFragment(){
    }    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
                             捆绑savedInstanceState){
        查看rootView = inflater.inflate(R.layout.fragment_home,集装箱,FALSE);        setHomeIcons(rootView);        返回rootView;
    }    公共无效setHomeIcons(查看rootView){
        bookIcon =(ImageView的)rootView.findViewById(R.id.bookIcon);
        bookIcon.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                //启动所有产品活动
                意图I =新意图(getActivity()getApplicationContext(),BrowseUniCourseActivity.class。);
                startActivity(ⅰ);
            }
        });        酒吧codeIcon =(ImageView的)rootView.findViewById(R.id.bar codeIcon);
        酒吧codeIcon.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                Snackbar.make(V,将打开栏code'的意图,Snackbar.LENGTH_LONG)
                        .setAction(动作,NULL).show();
            }
        });        messageIcon =(ImageView的)rootView.findViewById(R.id.messageIcon);
        messageIcon.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                Snackbar.make(V,将打开我的信息意愿,Snackbar.LENGTH_LONG)
                        .setAction(动作,NULL).show();            }
        });        listIcon =(ImageView的)rootView.findViewById(R.id.listIcon);
        listIcon.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                Snackbar.make(V,将打开我的房源意愿,Snackbar.LENGTH_LONG)
                        .setAction(动作,NULL).show();
            }
        });
    }
}

activity_main.xml中

 <?XML版本=1.0编码=UTF-8&GT?;
< android.support.v4.widget.DrawerLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:ID =@ + ID / drawer_layout
    机器人:背景=#FFF
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:fitsSystemWindows =真
    工具:openDrawer =开始>    <的LinearLayout
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:方向=垂直>        <包括
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            布局=@布局/ app_bar_main/>        <的FrameLayout
            机器人:ID =@ + ID / main_container
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent>
        < /&的FrameLayout GT;
    < / LinearLayout中>    < android.support.design.widget.NavigationView
        机器人:ID =@ + ID / nav_view
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =match_parent
        机器人:layout_gravity =开始
        机器人:fitsSystemWindows =真
        机器人:背景=@彩色/ colorPrimaryDark
        应用:itemTextColor =#FFF
        应用:itemIconTint =#FFF
        应用:headerLayout =@布局/ nav_header_main
        应用:菜单=@菜单/ activity_main_drawer/>< /android.support.v4.widget.DrawerLayout>

fragment_home.xml

 <?XML版本=1.0编码=UTF-8&GT?;
    < RelativeLayout的
        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent>        <! - 书图标和文本 - >
        < ImageView的
            机器人:ID =@ + ID / bookIcon
            机器人:layout_width =100dip
            机器人:layout_height =100dip
            机器人:layout_marginLeft =50dip
            机器人:layout_marginTop =110dip
            机器人:SRC =@绘制/书/>        <的TextView
            机器人:ID =@ + ID / bookLabel
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_below =@ + ID / bookIcon
            机器人:layout_marginLeft =20dip
            机器人:文字=@字符串/ bookLabel
            机器人:文字颜色=@彩色/ colorAlt
            机器人:文字样式=大胆/>
        <! - 酒吧code图标和文本 - >
        < ImageView的
            机器人:ID =@ + ID /酒吧codeIcon
            机器人:layout_width =95dip
            机器人:layout_height =95dip
            机器人:layout_marginLeft =260dip
            机器人:layout_marginTop =120dip
            机器人:SRC =@绘制/酒吧code/>        <的TextView
            机器人:ID =@ + ID /酒吧codeLabel
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_below =@ + ID /酒吧codeIcon
            机器人:layout_marginLeft =260dip
            机器人:paddingTop =5dip
            机器人:文字=@字符串/酒吧codeLabel
            机器人:文字颜色=@彩色/ colorAlt
            机器人:文字样式=大胆/>
        <! - 留言图标和文本 - >
        < ImageView的
            机器人:ID =@ + ID / messageIcon
            机器人:layout_width =100dip
            机器人:layout_height =100dip
            机器人:layout_marginLeft =50dip
            机器人:layout_marginTop =350dip
            机器人:SRC =@绘制/邮件/>        <的TextView
            机器人:ID =@ + ID / messageLabel
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_below =@ + ID / messageIcon
            机器人:layout_marginLeft =55dip
            机器人:文字=@字符串/ messageLabel
            机器人:文字颜色=@彩色/ colorAlt
            机器人:文字样式=大胆/>        <! - 列表图标和文本 - >
        < ImageView的
            机器人:ID =@ + ID / listIcon
            机器人:layout_width =95dip
            机器人:layout_height =95dip
            机器人:layout_marginLeft =260dip
            机器人:layout_marginTop =350dip
            机器人:SRC =@绘制/目录/>        <的TextView
            机器人:ID =@ + ID / listLabel
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_below =@ + ID / listIcon
            机器人:layout_marginLeft =273dip
            机器人:paddingTop =5dip
            机器人:文字=@字符串/ listLabel
            机器人:文字颜色=@彩色/ colorAlt
            机器人:文字样式=大胆/>    < / RelativeLayout的>


解决方案

好吧,我想我明白了。移动包括你的LinearLayout以外

 <包括
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        布局=@布局/ app_bar_main/>
<的LinearLayout
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直>
    <的FrameLayout
        机器人:ID =@ + ID / main_container
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent>
    < /&的FrameLayout GT;
< / LinearLayout中>

I have created an activity and tried to insert a fragment into the activity, but when running it the fragment does not appear when the activity is created. Just a blank space, there are no build errors.

Thanks for any help in advance.

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>

解决方案

Ok I think i got it. Move the include to outside of your 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>

这篇关于片段加入到活性,但不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆