简体   繁体   中英

Why is my TextView not centerInParent in my ViewPager fragment?

My TextView has layout_centerInParent="true" and looks good in the layout preview in Android Studio, but when I compile and run the "Let's Get Started" text ends up near the bottom as shown below. Why is this happening?

在此处输入图片说明

Fragment XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/intro_background"
    android:background="@color/primary">

    <TextView
        android:id="@+id/tv_welcome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:maxWidth="240sp"
        android:gravity="center"
        android:textSize="45sp"
        android:textStyle="bold"
        android:textColor="@color/white"
        android:text="Let\'s Get Started!"

        />

    <Button
        android:id="@+id/btn_add_course"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/tv_not_now"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="15dp"
        android:background="@drawable/btn_primary_with_white"
        android:paddingBottom="10dp"
        android:paddingLeft="40dp"
        android:paddingRight="40dp"
        android:paddingTop="10dp"
        android:text="Add My First Course"
        android:textColor="@color/white"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tv_not_now"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="40dp"
        android:maxWidth="280sp"
        android:layout_gravity="center_horizontal"
        android:gravity="left"
        android:textStyle="italic"
        android:textSize="16sp"
        android:textColor="@color/white"
        android:text="@string/intro_not_now"

        />


</RelativeLayout>

Activity XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        android:adjustViewBounds="true"
        android:src="@drawable/nav_header1" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:elevation="3dp"
        android:layout_marginTop="-25dp"
        >

    </android.support.v4.view.ViewPager>
</LinearLayout>

Activity:

private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_intro);
    viewPager = (ViewPager) findViewById(R.id.viewpager);
    viewPager.setAdapter(new Adapter_Intro(getSupportFragmentManager()));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
    }
}

Fragment:

public class IntroFragment extends Fragment {

    private static final String BACKGROUND_COLOR = "backgroundColor";
    private static final String PAGE = "page";

    private int mBackgroundColor, mPage;

    public static IntroFragment newInstance(int backgroundColor, int page) {
        IntroFragment frag = new IntroFragment();
        Bundle b = new Bundle();
        b.putInt(BACKGROUND_COLOR, backgroundColor);
        b.putInt(PAGE, page);
        frag.setArguments(b);
        return frag;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (!getArguments().containsKey(BACKGROUND_COLOR))
            throw new RuntimeException("Fragment must contain a \"" + BACKGROUND_COLOR + "\" argument!");
        mBackgroundColor = getArguments().getInt(BACKGROUND_COLOR);

        if (!getArguments().containsKey(PAGE))
            throw new RuntimeException("Fragment must contain a \"" + PAGE + "\" argument!");
        mPage = getArguments().getInt(PAGE);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        // Select a layout based on the current page
        int layoutResId;
        switch (mPage) {
            case 0:
                layoutResId = R.layout.fragment_intro_welcome;
                break;
            default:
                layoutResId = R.layout.fragment_intro_start;
        }

        // Inflate the layout resource file
        View view = getActivity().getLayoutInflater().inflate(layoutResId, container, false);

        // Set the current page index as the View's tag (useful in the PageTransformer)
        view.setTag(mPage);

        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // Set the background color of the root view to the color specified in newInstance()
        View background = view.findViewById(R.id.intro_background);
        background.setBackgroundColor(mBackgroundColor);
    }

}

Your TextView is well centered inside the fragment , but not inside the activity .

Because in your activity, there is the ImageView that takes some height in the top.

在此处输入图片说明

It seems that Android Studio is not accurate while rendering your layout.

I "measured" your screenshot using Gimp - as you can see, TextView is positioned exactly to the center of it parent, between top picture and bottom: 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM