我是android的初学者,今天我创建了一个BottomNavigationVIEw活动,我想显示3个带有导航按钮的选项卡,所以我创建了3个片段,问题是添加片段后,BottomNavigationVIEw在顶部sIDe
中显示了什么我应该怎么做,如果我想像添加片段之前一样将BottomNavigationVIEw放在底部这是我的主要活动代码
package com.HackerinsIDe.jaisonjoseph.polysocial;import androID.os.Bundle;import androID.support.annotation.NonNull;import androID.support.design.Widget.BottomNavigationVIEw;import androID.support.design.Widget.TabLayout;import androID.support.v7.app.AppCompatActivity;import androID.vIEw.MenuItem;import androID.Widget.TextVIEw;public class MainActivity extends AppCompatActivity {private TextVIEw mTextMessage;public BottomNavigationVIEw.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationVIEw.OnNavigationItemSelectedListener() { @OverrIDe public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemID()) { case R.ID.navigation_home: tab1 radio = new tab1(); androID.support.v4.app.FragmentManager manager = getSupportFragmentManager(); manager.beginTransaction().replace(R.ID.container, radio, radio.getTag()).commit(); case R.ID.navigation_dashboard: tab2 radio1 = new tab2(); androID.support.v4.app.FragmentManager manager1 = getSupportFragmentManager(); manager1.beginTransaction().replace(R.ID.container, radio1, radio1.getTag()).commit(); case R.ID.navigation_notifications: tab3 radio2 = new tab3(); androID.support.v4.app.FragmentManager manager2 = getSupportFragmentManager(); manager2.beginTransaction().replace(R.ID.container, radio2, radio2.getTag()).commit(); } return false; }};@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); tab1 radio = new tab1(); androID.support.v4.app.FragmentManager manager = getSupportFragmentManager(); manager.beginTransaction().replace(R.ID.container, radio, radio.getTag()).commit(); BottomNavigationVIEw navigation = (BottomNavigationVIEw) findVIEwByID(R.ID.navigation); navigation.setonNavigationItemSelectedListener(mOnNavigationItemSelectedListener);} }
这是activity_main.xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout 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/container"androID:layout_wIDth="match_parent"androID:layout_height="match_parent"androID:orIEntation="vertical"tools:context="com.HackerinsIDe.jaisonjoseph.polysocial.MainActivity"><FrameLayout androID:ID="@+ID/content" androID:layout_wIDth="match_parent" androID:layout_height="0dp" androID:layout_weight="1" androID:background="@androID:color/holo_blue_dark"> <TextVIEw androID:ID="@+ID/message" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_marginBottom="@dimen/activity_vertical_margin" androID:layout_marginleft="@dimen/activity_horizontal_margin" androID:layout_marginRight="@dimen/activity_horizontal_margin" androID:layout_margintop="@dimen/activity_vertical_margin" androID:text="@string/Title_home" /></FrameLayout><androID.support.design.Widget.BottomNavigationVIEw androID:ID="@+ID/navigation" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_gravity="bottom" androID:background="?androID:attr/windowBackground" app:menu="@menu/navigation" />
这是我的第一个片段tab1
<FrameLayout 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"xmlns:app="http://schemas.androID.com/apk/res-auto"tools:context="com.HackerinsIDe.jaisonjoseph.polysocial.tab1"><!-- Todo: Update blank fragment layout --><TextVIEw androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:text="@string/hello_blank_fragment" /><button androID:ID="@+ID/button" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="button" />
解决方法:
BottomNavigationVIEw不会自动显示在视图底部.您必须手动放置它们.
您可以为此使用relativeLayout.
<?xml version="1.0" enCoding="utf-8"?><relativeLayout 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/container" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" tools:context="com.HackerinsIDe.jaisonjoseph.polysocial.MainActivity"> <FrameLayout androID:ID="@+ID/content" androID:layout_wIDth="match_parent" androID:layout_height="0dp" androID:layout_weight="1" androID:background="@androID:color/holo_blue_dark"> <TextVIEw androID:ID="@+ID/message" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_marginBottom="@dimen/activity_vertical_margin" androID:layout_marginleft="@dimen/activity_horizontal_margin" androID:layout_marginRight="@dimen/activity_horizontal_margin" androID:layout_margintop="@dimen/activity_vertical_margin" androID:text="@string/Title_home" /> </FrameLayout> <androID.support.design.Widget.BottomNavigationVIEw androID:ID="@+ID/navigation" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_gravity="bottom" androID:background="?androID:attr/windowBackground" androID:layout_alignParentBottom="true" app:menu="@menu/navigation" /></relativeLayout>
如果将您的根linearLayout更改为relativeLayout并将参数androID:layout_alignParentBottom =“ true”添加到您的BottomNavigationVIEw中.希望对您有所帮助.
如果您需要更多帮助,则可能该链接会有所帮助:https://medium.com/@hitherejoe/exploring-the-android-design-support-library-bottom-navigation-drawer-548de699e8e0
总结以上是内存溢出为你收集整理的添加片段后,BottomNavigationView不在底部全部内容,希望文章能够帮你解决添加片段后,BottomNavigationView不在底部所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)