2013-08-01 54 views
0

我試圖解決這一段時間,並找不到問題。我有一個片段,我有ProgressBar中心。這裏是fragment的xml文件內容。Android中的片段內容位置

<merge xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" > 

    <ProgressBar 
     android:id="@+id/progress_bar" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" /> 

    <LinearLayout 
     android:id="@+id/details_form" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:clickable="false" 
     android:orientation="vertical" 
     android:visibility="gone" > 

     ... 
    </LinearLayout> 
</merge> 

我也有活動,我只是將此片段添加到活動中。下面是活動

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/details_container" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_gravity="center" 
    tools:context=".MyActivity" 
    tools:ignore="MergeRootFrame" /> 

問題的XML格式,當我瀏覽到我的應用程序內該活動的進展左側所示,不居中。我不知道爲什麼會發生這種情況,所以我認爲有人會看到我缺少的東西。謝謝。

編輯:

在我的活動代碼(MyActivity.java)OnCreate事件我有這樣的代碼:

DetailsFragment detailsFragment = new DetailsFragment(); 
      agreementDetailsFragment.setArguments(arguments); 
      getSupportFragmentManager() 
        .beginTransaction() 
        .add(R.id.details_container, 
          detailsFragment).commit(); 

,並在我的片段的.java文件中onCreateView我有這樣的代碼

View rootView = null; 
     /* 
     * Get root view 
     */ 
     LinearLayout wrapper = new LinearLayout(getActivity()); 
     inflater.inflate(R.layout.details_fragment, wrapper, true); 
     rootView = wrapper; 
+0

你究竟如何在片段中使用該佈局(合併的佈局)? – Luksprog

+0

我編輯了我的問題。 – Mediha

+0

您是否嘗試過使用RelativeLayout和android:layout_centerHorizo​​nal =「true」android:layout_centerVertical =「true」 – tophernuts

回答

0

問題是當我導航到我的應用程序 內的這個活動時,進度顯示在左邊一邊,它不居中。我不是 爲什麼會發生這種情況,所以我想有人會看到我丟失的東西 。謝謝

使用merge標記的佈局將作爲父項LinearLayout。在LinearLayoutandroid:layout_gravity將無法​​正常工作,你需要一種更加寬容的佈局像一個RelativeLayoutFrameLayout(我假設ProgressBardetails_formLinearLayout將使用visibility屬性相互替換):

RelativeLayout wrapper = new RelativeLayout(getActivity()); 
inflater.inflate(R.layout.details_fragment, wrapper, true); 
+0

我試過了,現在進度是在左上角(在此之前它是垂直居中但不是水平)。 – Mediha

+0

我將FrameLayout的fragment xml更改爲RelativeLayout,而改爲android:layout_gravity我使用了android:gravity和它居中的進度條。多謝你們。 – Mediha