Android对齐底部无法正常工作

时间:2016-09-02 20:35:43

标签: android android-layout android-fragments

我正在做一个Android应用程序,我需要在选项卡中将片段的布局与导航视图的应用栏底部对齐。问题是imageView出现在应用栏上方(而不是android:layout_alignBottom =“@ id / appbar”),并且在adittion中ScrollView不起作用。

我有导航视图,其中包含以下代码:

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();

    Fragment currFragment = null;
    FragmentManager fragmentManager = getSupportFragmentManager();

    if (id == R.id.nav_principal) {
        currFragment = new FastAccessFragment();
    } else if (id == R.id.nav_logout) {
        editor.putBoolean("signedUp", false);
        editor.apply();
        Intent intent = new Intent(this, LoginActivity.class);
        if (intent != null) {
            this.finish();
            startActivity(intent);
        }
    }

    if (currFragment != null) {
        fragmentManager
                .beginTransaction()
                .replace(R.id.content_main, currFragment)
                .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                .commit();
    }

    setTitle(item.getTitle());

content_main布局具有以下代码:

<?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/content_main_d"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/app_bar_main">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_fragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>


</RelativeLayout>

然后,在Fragment FastAccessFragment中,我创建了三个选项卡及其相应的选项卡,如下所示:

public class FastAccessFragment extends Fragment {

    private ViewPager viewPager;
    private AppBarLayout appBar;
    View rootview;
    TabLayout tabLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootview = inflater.inflate(R.layout.fast_access_fragment, container, false);

        if (savedInstanceState == null) {

            View parent = (View) container.getParent();
            appBar = (AppBarLayout) parent.findViewById(R.id.appbar);
            tabLayout = new TabLayout(getActivity());
            tabLayout.setTabTextColors(Color.parseColor("#C0C0C0"), Color.parseColor("#FFFFFF"));
            tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#FFA500"));
            appBar.addView(tabLayout);

            viewPager = (ViewPager) rootview.findViewById(R.id.pager_fragment);

            Adapter adapter = new Adapter(getFragmentManager());

            adapter.addFragment(new VoiceFragment(), "Voice");
            adapter.addFragment(new ButtonsFragment(), "Buttons");
            adapter.addFragment(new PicturesFragment(), "Pictures");

            viewPager.setAdapter(adapter);

            tabLayout.setupWithViewPager(viewPager);

            tabLayout.getTabAt(0).setIcon(R.drawable.microphone);
            tabLayout.getTabAt(1).setIcon(R.drawable.hand_up);
            tabLayout.getTabAt(2).setIcon(R.drawable.frame);
        }

        return rootview;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        appBar.removeView(tabLayout);
    }

    public class Adapter extends FragmentStatePagerAdapter {
        private final List<Fragment> fragments = new ArrayList<>();
        private final List<String> fragmentTitles = new ArrayList<>();

        public Adapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public android.support.v4.app.Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        public void addFragment(android.support.v4.app.Fragment fragment, String title) {
            fragments.add(fragment);
            fragmentTitles.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return fragmentTitles.get(position);
        }
    }

}

ButtonsFragment具有以下代码:

    public class ButtonsFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_buttons, container, false);

        Button up = (Button) view.findViewById(R.id.up_button);
        Button up_2 = (Button) view.findViewById(R.id.up_button_2);
        Button up_3 = (Button) view.findViewById(R.id.up_button_3);
        Button up_4 = (Button) view.findViewById(R.id.up_button_4);
        Button up_5 = (Button) view.findViewById(R.id.up_button_5);

        final ImageView im = (ImageView) view.findViewById(R.id.blind_image);

        up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                im.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.blind0, null));
            }
        });

        up_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                im.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.blind1, null));
            }
        });

        up_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                im.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.blind2, null));
            }
        });

        up_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                im.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.blind3, null));
            }
        });

        up_5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                im.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.blind4, null));
            }
        });

        return view;
    }
}

最后我有了一个片段fragment_buttons,其中ScrollView和alignBottom不起作用:

<LinearLayout 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"
    android:layout_alignBottom="@id/appbar"
    tools:context="com.blindsiot.carlos.blindsiot.VoiceFragment">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:id="@+id/blind_image"
                android:layout_gravity="center"
                android:src="@drawable/blind0" />

            <Button
                android:id="@+id/up_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                style="@style/Widget.AppCompat.Button.Colored"
                android:text="PRUEBA"/>

            <Button
                android:id="@+id/up_button_2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                style="@style/Widget.AppCompat.Button.Colored"
                android:text="PRUEBA"/>

            <Button
                android:id="@+id/up_button_3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                style="@style/Widget.AppCompat.Button.Colored"
                android:text="PRUEBA"/>

            <Button
                android:id="@+id/up_button_4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                style="@style/Widget.AppCompat.Button.Colored"
                android:text="PRUEBA"/>

            <Button
                android:id="@+id/up_button_5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                style="@style/Widget.AppCompat.Button.Colored"
                android:text="PRUEBA"/>

        </LinearLayout>

    </ScrollView>

</LinearLayout>

您可以在此处查看结果:enter image description here

预期的结果将是按钮的相同布局中的应用栏上方的图像视图,以及滚动视图中的所有图像视图。

与寻呼机对应并且使FastAccessFragment膨胀的布局具有以下代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

包含应用栏的app_bar_main布局的代码如下:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.blindsiot.carlos.blindsiot.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        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>

    <RelativeLayout
        android:id="@+id/content_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

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

2 个答案:

答案 0 :(得分:0)

您尚未定义父级LinearLayout的方向

<LinearLayout 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"
    android:orientation="vertical"
    android:layout_alignBottom="@id/appbar"
    tools:context="com.blindsiot.carlos.blindsiot.VoiceFragment">

并将ScrollView内的LinearLayout的高度设置为match_parent

答案 1 :(得分:0)

VoicePictures片段看起来不错吗?我认为你的问题不是(仅)在片段布局本身。

您可以分享一级的布局(具有ViewPagerAppBar的布局)吗?我怀疑问题出在你的ViewPager约束上。 ViewPager可能应该android:layout_alignBottom="@id/appbar",但在看到布局之前我无法确定。

您可以将android:layout_alignBottom与兄弟姐妹的元素一起使用,这不是这种情况 - 显然您的片段在ViewPager内(更不用说片段创建的包装器视图)。 / p>