2017-04-21 74 views
-1

我正在尝试将我的进度条居中在fragements屏幕中。以下是我迄今为止尝试过,在我看来有什么建议立即进行删除是正确的:居中进度条

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 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" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:orientation="vertical"> 

     <ProgressBar 
      android:id="@+id/pgFragment" 
      style="?android:attr/progressBarStyleLarge" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center"/> 

    </LinearLayout> 

    <ScrollView 
     android:id="@+id/scrollView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:visibility="gone"> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/recyclerView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      tools:listitem="@layout/recycler_row" /> 

    </ScrollView> 

</LinearLayout> 

我试图删除包装设计,摆脱了整个ScrollView(但随着visibility设置为gone,它不该”问题)。目前的结果是,ProgressBar粘在屏幕的顶部。

可能是因为活动布局实施NestedScrollView

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.NestedScrollView 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" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.mediasaturn.productlistpoc.activities.MainActivity" 
    tools:showIn="@layout/activity_main"> 

    <fragment android:name="com.productlistpoc.recyclerview.RecyclerViewFragment" 
     android:id="@+id/fragment_recycler" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:layout="@layout/fragment_recycler" /> 

</android.support.v4.widget.NestedScrollView> 
+0

让父布局相对布局 –

+0

然后呢?只有这不会改变任何东西。将根布局和进度条父级更改为相对 – 4ndro1d

+0

并使进度条父级线性布局:centerinparent = true –

回答

0

更新你这样的代码

<?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"> 
    <LinearLayout 
     android:id="@+id/linearLayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/recyclerView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      tools:listitem="@layout/recycler_row" /> 

    </LinearLayout> 
<ProgressBar 
      android:id="@+id/pgFragment" 
      style="?android:attr/progressBarStyleLarge" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true"/> 
</RelativeLayout> 
+0

可悲的是,这也是行不通的。仍然停留在屏幕上方。可能与NestedScrollView有关?看编辑的问题 – 4ndro1d

+0

尝试编辑的代码 –

0
<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 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" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:orientation="vertical"> 

     <ProgressBar 
      android:id="@+id/pgFragment" 
      style="?android:attr/progressBarStyleLarge" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center"/> 

    </LinearLayout> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recyclerView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 


</FrameLayout> 
+0

这也不起作用 - 编辑我的问题。可能与NestedScrollView有关? – 4ndro1d

0

不要使用RecyclerViewScrollView。您已将NestedScrollView用作容器layout,因此您无需添加ScrollView

更新你的XML如下:

<?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"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recyclerView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

    </android.support.v7.widget.RecyclerView> 

    <RelativeLayout 
     android:id="@+id/layoutProgress" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <ProgressBar 
      android:id="@+id/pgFragment" 
      style="?android:attr/progressBarStyleLarge" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true"/> 

    </RelativeLayout> 
</RelativeLayout> 

#。如果要显示/隐藏prograssbar,只需showhidelayoutProgress以编程方式使用layoutProgress.setVisibility(View.VISIBLE)layoutProgress.setVisibility(View.GONE)方法。

希望这会有所帮助〜