【问题标题】:Viewpager fragments not overlapping activity elementsViewpager 片段不重叠活动元素
【发布时间】:2016-06-16 19:02:39
【问题描述】:

我有一个活动,我有一些图像、文本视图、一个按钮和 viewpager。 Viewpager 具有与父级匹配的高度和宽度,并且所有布局的背景都为白色。现在,当活动加载时,viewpager 通过适配器获取其片段并加载片段。现在图像,文本视图隐藏在加载的片段后面,但按钮不会隐藏并且显示在片段中。为什么会这样?

这是我的活动布局,viewpager 作为最后一个元素

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context="com.appointmentno.android.LogoFrontScreen"
android:background="@android:color/white"
android:keepScreenOn="true">

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:id="@+id/rel_center_logo">

    <ImageView
        android:layout_width="@dimen/logo_width"
        android:layout_height="@dimen/logo_height"
        android:src="@drawable/finalc"
        android:scaleType="centerInside"
        android:layout_centerHorizontal="true"
        android:id="@+id/img_logo_c"/>
    <TextView
        android:layout_width="210dp"
        android:layout_height="wrap_content"
        android:text="@string/logo_name"
        android:layout_below="@id/img_logo_c"
        android:id="@+id/app_name"
        style="@style/RobotoTextViewStyle"
        android:layout_centerHorizontal="true"
        android:gravity="center_horizontal"
        android:textSize="@dimen/logo_text_size"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/logo_underline"
        android:layout_below="@id/app_name"
        android:id="@+id/app_name_underline"
        style="@style/RobotoTextViewStyle"
        android:textColor="@android:color/black"
        android:textSize="13sp"
        android:layout_marginTop="10dp"
        android:layout_centerHorizontal="true"/>
</RelativeLayout>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/txt_progress_report"
    android:layout_below="@+id/rel_center_logo"
    android:gravity="center"
    android:layout_marginTop="50dp"
    android:layout_centerHorizontal="true"
    android:maxLines="10"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/str_2"
    android:id="@+id/btn_enter_app"
    android:layout_below="@id/rel_center_logo"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="84dp"/>

<ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:indeterminate="true"
    android:id="@+id/logo_screen_progress"
    android:layout_below="@id/rel_center_logo"
    android:layout_marginTop="10dp"
    android:layout_centerHorizontal="true"/>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:visibility="gone"
    android:background="@color/colorAccent"
    android:textColor="@android:color/white"
    android:id="@+id/txt_ver_alert"
    android:gravity="center"/>

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager_gallery"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="gone"/>
</RelativeLayout>

【问题讨论】:

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


    【解决方案1】:

    试试这个:

    <RelativeLayout
        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"
        tools:context="com.appointmentno.android.LogoFrontScreen"
        android:background="@android:color/white"
        android:keepScreenOn="true"
        android:gravity="center">
    
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:id="@+id/rel_center_logo"
        android:orientation="vertical">
    
    <ImageView
        android:layout_width="@dimen/logo_width"
        android:layout_height="@dimen/logo_height"
        android:src="@drawable/finalc"
        android:scaleType="centerInside"
        android:layout_centerHorizontal="true"
        android:id="@+id/img_logo_c"/>
    <TextView
        android:layout_width="210dp"
        android:layout_height="wrap_content"
        android:text="@string/logo_name"
        android:id="@+id/app_name"
        style="@style/RobotoTextViewStyle"
        android:layout_centerHorizontal="true"
        android:gravity="center_horizontal"
        android:textSize="@dimen/logo_text_size"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/logo_underline"
        android:id="@+id/app_name_underline"
        style="@style/RobotoTextViewStyle"
        android:textColor="@android:color/black"
        android:textSize="13sp"
        android:layout_marginTop="10dp"
        android:layout_centerHorizontal="true"/>
    </LinearLayout>
    
     <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:id="@+id/rel_center_logo"
        android:orientation="vertical"
        android:layout_below="@id/rel_center_logo">
    
    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/txt_progress_report"
    android:gravity="center"
    android:layout_marginTop="50dp"
    android:layout_centerHorizontal="true"
    android:maxLines="10"/>
    
    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/str_2"
    android:id="@+id/btn_enter_app"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="84dp"/>
    
    <ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:indeterminate="true"
    android:id="@+id/logo_screen_progress"
    android:layout_marginTop="10dp"
    android:layout_centerHorizontal="true"/>
    
    <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="gone"
    android:background="@color/colorAccent"
    android:textColor="@android:color/white"
    android:id="@+id/txt_ver_alert"
    android:gravity="center"/>
    </LinearLayout>
    
    <android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager_gallery"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="gone"/>
    </RelativeLayout>
    

    我基本上将您的视图分为几组(根据我认为您想要的)并将它们放入各自的 LinearLayouts 中。所以最后你有一个RelativeLayout,它包含两个LinearLayouts和一个ViewPager。

    【讨论】:

    • @shreyansjain 我不完全确定为什么会显示该按钮,但我注意到您对layout_below="someID" 有一些查看,所以为了清理它们,我只需将它们放入LinearLayout因为这就是它们的含义:在一个方向上对齐视图而没有重叠。所以使用RelativeLayout 是没有意义的。只有拥有所有内容的主要RelativeLayout 才能获得所需的布局。如果它对您有用,请将此答案标记为已接受。
    • 好的,我还没有测试它,如果它运行我会标记为正确的。
    • @shreyansjain 我明白了。如果它不起作用,请告诉我,我会再看看它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多