android:layout_height="fill_parent" from Viewpager does not work

Asked

Viewed 78 times

0

I have a ViewPage that does not work the android:layout_height="fill_parent", shows nothing on the screen, but if I determine an ex value: android:layout_height="500dp" the determined height appears.

.xml

<RelativeLayout
    android:id="@+id/main_layout"
    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"
    tools:context=".MainActivity">

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:elevation="6dp"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/page"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" <--não trabalha
        android:layout_below="@id/tab_layout" />

</RelativeLayout>

.java

public class AlterarOsFragment extends Fragment implements View.OnClickListener {
    public static final String TAG = "";
    DataBaseHandler db;
    int numeroOs;


    public AlterarOsFragment(int numeroOs) {
        this.numeroOs = numeroOs;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_alterar_os, container, false);
        db = new DataBaseHandler(getActivity());

        TabLayout tabLayout = (TabLayout) rootView.findViewById(R.id.tab_layout);
        tabLayout.addTab(tabLayout.newTab().setText(getResources().getString(R.string.informacoes)));
        tabLayout.addTab(tabLayout.newTab().setText(getResources().getString(R.string.servicos)));
        tabLayout.addTab(tabLayout.newTab().setText(getResources().getString(R.string.orcamento)));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

        final ViewPager viewPager = (ViewPager) rootView.findViewById(R.id.page);
        final PagerAdapter adapter = new TabAdapter(getActivity().getSupportFragmentManager(), tabLayout.getTabCount(), numeroOs);

        viewPager.setAdapter(adapter);


        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
                }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onClick(View view) {

    }
}
  • What’s inside that one TabAdapter? From what I understand, you’re creating a ViewPager within aFragment right? How is your xml view configured within Tabadapter?

1 answer

0

It is recommended that developers use the new "match_parent" template. And for older applications, of course compatibility continues to exist. Try changing your layout to match_parent.

android:layout_width="match_parent"

What is the match_parent function ?

The match_parent function is to give a special value to the height or width requested by a View.

Use match_parent because there is no difference if you are in versions of API 8+,and since mobile phones => Android 2.2 is advisable to use match_parent, but for compatibility reasons for previous versions there is still fill_parent.

OBS : In Android API versions from 1.6 to 2.1 is advisable use fill_parentbecause using match_parent in these versions occur certain errors,then in these versions of API use fill_parent.

According to the official documentation of Android itself, it specifies that are the same thing and that there is no difference in using either.

https://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html

  • I don’t understand the answer, you mean if used match_parent the problem indicated in the question is solved?

  • 1

    I’d already tried it match_parent but it didn’t work either.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.