为什么android导航抽屉创建两个标题?

为什么android导航抽屉创建两个标题?,第1张

概述我按照教程将登录时的facebook信息推送到带有导航的活动,然后将其放入顶部,如图所示,经过几天的更改代码后,我终于开始工作了.但现在我不能让原来的标题消失.我已经尝试改变代码的每一部分,但我总是最终得到全部或全部.我知道两个人比一个人好,但这是一个例外.此外,我想我不能发

我按照教程将登录时的facebook信息推送到带有导航的活动,然后将其放入顶部,如图所示,经过几天的更改代码后,我终于开始工作了.
但现在我不能让原来的标题消失.我已经尝试改变代码的每一部分,但我总是最终得到全部或全部.我知道两个人比一个人好,但这是一个例外.

此外,我想我不能发布图像,因此,原始标题位于顶部,它应该在哪里.
我的脸谱资料图片和信息的新标题就在下面.
在下面,启动导航抽屉菜单.

public class HomeActivity extends AppCompatActivity    implements NavigationVIEw.OnNavigationItemSelectedListener {JsONObject response, profile_pic_data, profile_pic_url;TextVIEw user_name, user_email, tokens;ImageVIEw user_picture;NavigationVIEw navigation_vIEw;String name;button button2;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_home);    tokens = (TextVIEw)findVIEwByID(R.ID.textVIEw17);    button2 = (button)findVIEwByID(R.ID.button2);    Toolbar toolbar = (Toolbar) findVIEwByID(R.ID.toolbar);    setSupportActionbar(toolbar);    Intent intent = getIntent();    String Jsondata = intent.getStringExtra("Jsondata");    final String uID = intent.getStringExtra("UID");    setNavigationheader();    // call setNavigationheader Method.    setUserProfile(Jsondata, uID);    floatingActionbutton fab = (floatingActionbutton) findVIEwByID(R.ID.fab);            fab.setVisibility(VIEw.INVISIBLE);    fab.setonClickListener(new VIEw.OnClickListener() {        @OverrIDe        public voID onClick(VIEw vIEw) {            Firebase ref = new Firebase("https://luckycashslots.firebaseio.com/data/users/" + MainActivity.uID + "/");            Tokens token = new Tokens("100");            ref.setValue(token);            Snackbar.make(vIEw, "Replace with your own action", Snackbar.LENGTH_LONG)                    .setAction("Action", null).show();        }    });    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);    //Toast.makeText(getApplicationContext(), uID, Toast.LENGTH_LONG).show();    button2.setonClickListener(new VIEw.OnClickListener() {        @OverrIDe        public voID onClick(VIEw vIEw) {           // Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();        updateText();        }    });    Firebase ref = new Firebase("https://luckycashslots.firebaseio.com/data/users/" + MainActivity.uID + "/");    Firebase tokRef = ref.child("tokens");    //tokRef.setValue(mAuthData.getProvIDer());    //Tokens token = new Tokens(100);    //ref.setValue(token);    ref.addValueEventListener(new ValueEventListener() {        @OverrIDe        public voID onDataChange(DataSnapshot dataSnapshot) {            if (dataSnapshot.child("tokens").getValue() != null) {                name = (String) dataSnapshot.child("tokens").getValue().toString();                tokens.setText(name);                //tokens.setText(dataSnapshot.getValue().toString());                //  Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();                // System.out.println(dataSnapshot.getValue());                // String woot = dataSnapshot.getValue().toString();                // tokens.setText(woot);            }        }        @OverrIDe        public voID onCancelled(FirebaseError firebaseError) {            Toast.makeText(getApplicationContext(), "Couldnt update token text", Toast.LENGTH_LONG).show();        }    });}public voID updateText(){    Firebase ref = new Firebase("https://luckycashslots.firebaseio.com/data/users/" + MainActivity.uID + "/");    Firebase tokRef = ref.child("tokens");    //tokRef.setValue(mAuthData.getProvIDer());   // Tokens token = new Tokens(100);  //  ref.setValue(token);    ref.addValueEventListener(new ValueEventListener() {        @OverrIDe        public voID onDataChange(DataSnapshot dataSnapshot) {        for (DataSnapshot tokenSnapshot: dataSnapshot.getChildren()){            Tokens token = tokenSnapshot.getValue(Tokens.class);             System.out.println(token.toString());            name = token.toString();           // name = (String) dataSnapshot.child("tokens").getValue();            tokens.setText(name);        }       // name = (String) dataSnapshot.child("tokens").getValue().toString();       // tokens.setText(name);        }        @OverrIDe        public voID onCancelled(FirebaseError firebaseError) {            Toast.makeText(getApplicationContext(), "Couldnt update token text", Toast.LENGTH_LONG).show();        }    });}public voID setNavigationheader(){    navigation_vIEw = (NavigationVIEw) findVIEwByID(R.ID.nav_vIEw);    navigation_vIEw.removeheaderVIEw(null);    VIEw header = LayoutInflater.from(this).inflate(R.layout.nav_header_home, null);            navigation_vIEw.addheaderVIEw(header);    user_name = (TextVIEw) header.findVIEwByID(R.ID.username);    user_picture = (ImageVIEw) header.findVIEwByID(R.ID.profile_pic);    user_email = (TextVIEw) header.findVIEwByID(R.ID.email);}public voID setUserProfile(String Jsondata, String uID){    try    {        response = new JsONObject(Jsondata);        user_email.setText(response.get("email").toString());       // user_email.setText(MainActivity.uEmail);         user_name.setText(response.get("name").toString());       // user_name.setText(MainActivity.uname);        profile_pic_data = new JsONObject(response.get("picture").toString());        profile_pic_url = new JsONObject(profile_pic_data.getString("data"));        Picasso.with(this).load(profile_pic_url.getString("url")).into(user_picture);    }    catch            (Exception e) {        e.printstacktrace();    }}@OverrIDepublic voID onBackpressed() {    DrawerLayout drawer = (DrawerLayout) findVIEwByID(R.ID.drawer_layout);    if (drawer.isDrawerOpen(GravityCompat.START)) {        drawer.closeDrawer(GravityCompat.START);    } else {        super.onBackpressed();    }}@OverrIDepublic boolean onCreateOptionsMenu(Menu menu) {    // Inflate the menu; this adds items to the action bar if it is present.    getMenuInflater().inflate(R.menu.home, menu);    return true;}@OverrIDepublic 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;    }    return super.onoptionsItemSelected(item);}@SuppressWarnings("StatementWithEmptyBody")@OverrIDepublic boolean onNavigationItemSelected(MenuItem item) {    // Handle navigation vIEw item clicks here.    int ID = item.getItemID();    if (ID == R.ID.nav_slots) {        // Handle the camera action1        Intent intent2 = new Intent(this, SlotPageVIEw.class);        startActivity(intent2);    } else if (ID == R.ID.nav_spin) {        Intent intent2 = new Intent(this, DailySpinActivity.class);        startActivity(intent2);    } else if (ID == R.ID.nav_offers) {    } else if (ID == R.ID.nav_prizes) {        Intent intent2 = new Intent(this, PrizesActivity.class);        startActivity(intent2);    } else if (ID == R.ID.nav_winners) {        Intent intent2 = new Intent(this, WinnersActivity.class);        startActivity(intent2);    } else if (ID == R.ID.nav_stats) {    } else if (ID == R.ID.nav_account) {        Intent intent2 = new Intent(this, AccountActivity.class);        startActivity(intent2);    }    DrawerLayout drawer = (DrawerLayout) findVIEwByID(R.ID.drawer_layout);    drawer.closeDrawer(GravityCompat.START);    return true;}}

activity_home.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:layout_wIDth="match_parent"     androID:layout_height="match_parent"   androID:fitsSystemwindows="true" tools:openDrawer="start"><include layout="@layout/app_bar_home" androID:layout_wIDth="match_parent"    androID:layout_height="match_parent" /><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"    app:headerLayout="@layout/nav_header_home" app:menu="@menu/activity_home_drawer" />

App bar home xml

<?xml version="1.0" enCoding="utf-8"?><androID.support.design.Widget.CoordinatorLayoutxmlns: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" androID:fitsSystemwindows="true"tools:context="learn2crack.learn2crackfb.HomeActivity"><androID.support.design.Widget.AppbarLayout androID:layout_height="wrap_content"    androID:layout_wIDth="match_parent" androID:theme="@style/Apptheme.AppbarOverlay">    <androID.support.v7.Widget.Toolbar androID:ID="@+ID/toolbar"        androID:layout_wIDth="match_parent" androID:layout_height="?attr/actionbarSize"        androID:background="?attr/colorPrimary" app:popuptheme="@style/Apptheme.PopupOverlay" /></androID.support.design.Widget.AppbarLayout><include layout="@layout/content_home" /><androID.support.design.Widget.floatingActionbutton androID:ID="@+ID/fab"    androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content"    androID:layout_gravity="bottom|end" androID:layout_margin="@dimen/fab_margin"    androID:src="@androID:drawable/ic_dialog_email" /></androID.support.design.Widget.CoordinatorLayout>

导航头家xml

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:layout_wIDth="match_parent" androID:layout_height="@dimen/nav_header_height"androID:background="@drawable/sIDe_nav_bar"androID:paddingBottom="@dimen/activity_vertical_margin"androID:paddingleft="@dimen/activity_horizontal_margin"androID:paddingRight="@dimen/activity_horizontal_margin"androID:paddingtop="@dimen/activity_vertical_margin"androID:theme="@style/themeOverlay.AppCompat.Dark" androID:orIEntation="vertical"androID:gravity="bottom"><de.hdodenhof.circleimagevIEw.circleimageVIEw    androID:ID="@+ID/profile_pic"    androID:layout_wIDth="80dp"    androID:layout_height="80dp"    androID:paddingtop="@dimen/nav_header_vertical_spacing"    androID:src="@androID:drawable/sym_def_app_icon"    /><TextVIEw androID:ID="@+ID/username"androID:layout_wIDth="match_parent" androID:layout_height="wrap_content"    androID:paddingtop="@dimen/nav_header_vertical_spacing"    androID:text="AndroID Studio"    androID:textSize="14dp"androID:textAppearance="@style/TextAppearance.AppCompat.Body1"androID:textcolor="#000000" /><TextVIEw    androID:ID="@+ID/email"    androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content"    androID:text="androID.studio@androID.com" /></linearLayout>

解决方法:

使用app添加标题时:headerLayout =“@ layout / nav_header_home”就像下面一样

<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"    app:headerLayout="@layout/nav_header_home" app:menu="@menu/activity_home_drawer" />

然后无需再从代码中添加标题

从setNavigationheader()方法中删除以下行

navigation_vIEw.removeheaderVIEw(null);VIEw header = LayoutInflater.from(this).inflate(R.layout.nav_header_home, null);navigation_vIEw.addheaderVIEw(header);

删除后的方法如下所示

编辑/

public voID setNavigationheader(){    navigation_vIEw = (NavigationVIEw) findVIEwByID(R.ID.nav_vIEw);

VIEw header = LayoutInflater.from(this).inflate(R.layout.nav_header_home,null);

    user_name = (TextVIEw) navigation_vIEw.findVIEwByID(R.ID.username);    user_picture = (ImageVIEw) navigation_vIEw.findVIEwByID(R.ID.profile_pic);    user_email = (TextVIEw) navigation_vIEw. findVIEwByID(R.ID.email);}
总结

以上是内存溢出为你收集整理的为什么android导航抽屉创建两个标题?全部内容,希望文章能够帮你解决为什么android导航抽屉创建两个标题?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1108248.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存