为什么我的TextView在我的ViewPager片段中不是centerInParent?

时间:2015-10-24 20:07:54

标签: android android-layout android-fragments android-viewpager android-relativelayout

我的TextViewlayout_centerInParent="true",并且在Android Studio的布局预览中看起来不错,但是当我编译并运行"让我们开始使用"文本最终靠近底部,如下所示。为什么会这样?

enter image description here

片段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>

活动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>

活动:

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);
        }
    }
}

片段:

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);
    }

}

2 个答案:

答案 0 :(得分:2)

您的TextView位于片段内部,但不在活动内。

因为在您的活动中,ImageView在顶部有一些高度。

enter image description here

答案 1 :(得分:2)

在渲染布局时,似乎Android Studio不准确。

我使用Gimp“测量”了你的截图 - 正如你所看到的,TextView完全位于它的父级中心,在顶部图片和底部之间: enter image description here