android - 当我在布局末尾对齐选项卡布局时,布局顶部留下了一个空格

标签 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>

屏幕截图 enter image description here

最佳答案

空白区域是您的操作栏,您必须为其提供单独的布局,并在该布局中附加所需的 map 和人员。 将此代码写入您的 Activity 的 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();

希望这对您有帮助。

关于android - 当我在布局末尾对齐选项卡布局时,布局顶部留下了一个空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36335648/

相关文章:

android:windowTranslucentStatus/fitsSystemWindows 工具栏大小错误

android - HttpURLConnection 的 setRequestProperty 函数

sql-server - 从 XML 节点查询值(可能是 NAMESPACE 问题)

android - ListView 不显示项目

JAVA:使用 XPath 表达式构建 XML 文档

android - 在 Android 中开发 GUI 的好工具?

android - 我怎样才能拥有带有手动控制的水平listView或gridView

java - 手机锁定时检测Home按钮 'pressed'

android - 服务中的 BroadcastReceiver 不接收广播 Android

android - 检索 SQLite 表中的最后一条记录(再次)