当我在布局结束时对齐标签布局时,布局顶部会留下一个空格

时间:2016-03-31 14:01:16

标签: android xml android-layout android-fragments android-tablayout

我附加了java代码及其xml和截图。 tablayout.java是我附加tablaout.xml的类,当我在屏幕底部对齐tablayout时,布局顶部的一些空间被浪费了。

TabLayout.java

public class Bottom_Tabs_Activity extends AppCompatActivity {
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private int[] tabIcons = {
            R.drawable.ic_friends,
            R.drawable.ic_map,
            R.drawable.ic_status,
            R.drawable.ic_chat,
            R.drawable.ic_profile
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabs);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        if (viewPager != null)
            setupViewPager(viewPager);
        else {
            Log.e("test", "i am null");
        }
        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);

        setupTabIcons();
    }
        private void setupTabIcons() {
            tabLayout.getTabAt(0).setIcon(tabIcons[0]);
            tabLayout.getTabAt(1).setIcon(tabIcons[1]);
            tabLayout.getTabAt(2).setIcon(tabIcons[2]);
            tabLayout.getTabAt(3).setIcon(tabIcons[3]);
            tabLayout.getTabAt(4).setIcon(tabIcons[4]);
        }

    private void setupViewPager(ViewPager viewPager)
    {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFrag(new MapFragment(),"MAPS");
        adapter.addFrag(new PeopleFragment(),"PEOPLE");
        adapter.addFrag(new HomeFragment(),"HOME");
        adapter.addFrag(new ChatFragment(),"CHAT");
        adapter.addFrag(new ProfileFragment(),"PROFILE");

        viewPager.setAdapter(adapter);
    }
    class ViewPagerAdapter extends FragmentPagerAdapter
    {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();
        public ViewPagerAdapter(FragmentManager manager)
        {
            super(manager);
        }
        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }
        @Override
        public int getCount() {
            return mFragmentList.size();
        }
        public void addFrag(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }
        @Override
        public CharSequence getPageTitle(int position) {

            // return null to display only the icon
            return null;
        }
    }
}

tablayout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:gravity="bottom"
        android:layout_gravity="bottom">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            app:tabMode="fixed"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#000000"
            app:tabIndicatorColor="#ff1232"
            app:tabGravity="fill"
            />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

SCREENSHOT enter image description here

1 个答案:

答案 0 :(得分:2)

空白区域是您的操作栏,您必须为其提供单独的布局,并在该布局中附加所需的地图和人员。 在活动的setContentView下面编写此代码,并根据需要进行编辑。

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.black)));
        getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        getSupportActionBar().setCustomView(R.layout.action_bar);

        EditText s= (EditText) getSupportActionBar().getCustomView().findViewById(R.id.centertext2);
        s.setText("New Role");

或者,如果您要删除操作栏

getSupportActionBar().hide();

希望这会对你有所帮助。