android - View doesn't aligned in the center of layout -
i have simple layout, follow:
<?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"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centervertical="true" android:layout_centerhorizontal="true" android:text: "not centralized!" /> <android.support.v7.widget.recyclerview android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone" /> </relativelayout>
the above layout belongs fragment loaded inside viewpager (which belongs activity - called here activitya.java).
the activitya.java xml layout file following:
<?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.support.design.widget.appbarlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:statelistanimator="@animator/elevated_appbar" app:theme="@style/themeoverlay.appcompat.dark.actionbar"> <android.support.v7.widget.toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionbarsize" app:layout_scrollflags="scroll|enteralways" app:popuptheme="@style/themeoverlay.appcompat.light" /> <android.support.design.widget.tablayout android:id="@+id/tablayout" android:layout_width="match_parent" android:layout_height="?attr/actionbarsize" app:tabindicatorcolor="@android:color/white" app:tabmode="fixed" /> </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>
note i'm using coordinatorlayout , viewpager has app:layout_behavior="@string/appbar_scrolling_view_behavior"
attribute.
the little thing makes me confusing is: why textview not centralized on layout since i'm declaring android:layout_centervertical="true"
, android:layout_centervertical="true"
it?!
strangely, when remove app:layout_scrollflags="scroll|enteralways"
attribute on android.support.v4.view.viewpager content displayed on middle of screen, but... android.support.v7.widget.toolbar stops collapse , expand features.
edit
on layout preview, can see android.support.v4.view.viewpager
not above navigation bar (but app:layout_behavior="@string/appbar_scrolling_view_behavior"
needed put view bellow android.support.design.widget.appbarlayout
). when removing attribute, android.support.v4.view.viewpager
goes navigation bar, overlaps android.support.design.widget.appbarlayout
.
take look:
with app:layout_behavior="@string/appbar_scrolling_view_behavior"
:
without app:layout_behavior="@string/appbar_scrolling_view_behavior"
:
edit 2
so, decided test reproduce issue on well referenced project demonstrates material features , result is...
... same problem!
if wants reproduce yourself, change fragment_cheese_list.xml to:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.recyclerview android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone" /> <android.support.v7.widget.appcompattextview android:id="@+id/alertmessage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centervertical="true" android:layout_centerhorizontal="true" android:text="also, not centralized..." /> </relativelayout>
try adding app:layout_behavior="@string/appbar_scrolling_view_behavior"
fragment's layout xml file.
edit1
behaving supposed to. when have viewpager
with:app:layout_behavior="@string/appbar_scrolling_view_behavior"
android shifts viewpager
down, according height of appbarlayout
(might adding margin it). , margin readjusted when scroll view up. because scrolling_view_behavior
is
and without:app:layout_behavior="@string/appbar_scrolling_view_behavior"
your viewpager
independent of appbarlayout
hence, no margin thats why textview
appears in center in case.
so, in reality, textview in center of viewpager. viewpager shifts. changing behavior might need implement own layout behavior. this how android it.
Comments
Post a Comment