2017-04-21 78 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)方法。

希望這會有所幫助〜