2012-05-31 102 views
0

我主要佈局在這樣片段不工作

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" > 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 
    <Button 
     android:id="@+id/btn_showdetails" 
     android:layout_width="100dip" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="110dip" 
     android:layout_marginTop="15dip" 
     android:text="Show Details" /> 

    </LinearLayout> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" 
    android:id="@+id/my_parent_layout" 
    > 
    </LinearLayout> 



</LinearLayout> 

在嘗試使用這個類 包com.appnetics來調用my_parent_layout另一個活動;

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentTransaction; 
import android.view.View; 
import android.widget.Button; 

public class FragmenttestActivity extends FragmentActivity { 
    Button LogIn = null ; 
    final FragmentManager fragmentManager = getSupportFragmentManager() ; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     LogIn = (Button) findViewById(R.id.btn_showdetails); 
     LogInEvent() ; 
    } 
public void LogInEvent(){ 

     LogIn.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 


         FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
         details fragment = new details(); 
         fragmentTransaction.add(R.id.my_parent_layout, fragment); 
         fragmentTransaction.commit(); 


      } 
     }); 

    } 


} 

細節佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/date_time_label" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="200dp" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="32dp" 
     android:textStyle="bold" 

     android:text="time_label" 
     /> 

    <TextView 
     android:id="@+id/last_view_time" 
     android:layout_toRightOf="@+id/date_time_label" 
     android:layout_alignBaseline="@+id/date_time_label" 
     android:layout_marginLeft="8dip" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textSize="32dp" 

     /> 

</LinearLayout> 

和佈局代碼爲 /* $ ID:$ */

package com.appnetics; 

import java.text.SimpleDateFormat; 
import java.util.Date; 



import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 




/** 
* DateTime 
*/ 
public class details extends Fragment { 
    private static String getDateTimeString(Date time) { 
     return new SimpleDateFormat("d MMM yyyy HH:mm:ss") 
      .format(time); 
    } 

    private final String time = getDateTimeString(new Date()); 

    /** @see android.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle) */ 
    @Override 
    public View onCreateView(
     LayoutInflater inflater, 
     ViewGroup container, 
     Bundle b) 
    { 
     View view = inflater.inflate(
      R.layout.details, 
      container, 
      false); //!!! this is important 

     ((TextView) view.findViewById(R.id.last_view_time)) 
      .setText(time); 

     return view; 
    } 
} 

的問題是,當點擊該按鈕,什麼都沒有出現,任何想法我的代碼有什麼問題

+0

你試過:fragmentTransaction.replace(R.id.my_parent_layout,片段) – Tony

+0

你膨脹您的詳細信息視圖分段? – Tarun

+0

@Tony我會試試 – AMH

回答

6

讓我們開始!

在您的詳細信息XML:

​​3210

這兩個標籤是相對的佈局,佈局的家長必須以工作是相對的類型。

android:layout_centerVertical="true" 

這個標籤也是不正確的,如果你想中心在其父母或使用,您可以使用兩種方法或TextView的包裹內容TextView的文本,並設置中心:

android:gravity="right" 
android:width="fill_parent" 

這將是detail_fragment.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 
<TextView 
    android:id="@+id/date_time_label" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="32dp" 
    android:textStyle="bold" 
    android:text="time_label" 
    /> 

<TextView 
    android:id="@+id/last_view_time" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="right" 
    android:text="view time" 
    android:textSize="32dp" /> 

</LinearLayout> 

在您的main.xml中,您聲明模式很多次!應該在您的根佈局上聲明一次。

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

所以main.xml中會...

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:gravity="center_horizontal" 
android:orientation="vertical" > 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" /> 

<Button 
    android:id="@+id/add_fragment" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Button" /> 

<LinearLayout 
    android:id="@+id/fragment_holder" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

</LinearLayout> 

</LinearLayout> 

的片段是在類中聲明,並應用它的XML元素。

public class DetailsFragment extends Fragment { 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.fragment, container, false); 
} 
} 

,並在主活動將它添加到支架佈局:

public class FragmentSimpleTestActivity extends FragmentActivity { 

private Button addFragment; 


/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    final FragmentManager fragmentManager = getSupportFragmentManager(); 

    addFragment = (Button) findViewById(R.id.add_fragment); 

    addFragment.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      FragmentTransaction fragmentTransaction =  fragmentManager.beginTransaction(); 
      DetailsFragment fragment = new DetailsFragment(); 
      fragmentTransaction.add(R.id.fragment_holder,fragment, "MY_FRAG"); 
      fragmentTransaction.commit(); 
     } 
    }); 
} 
} 

其重要的fragmentTransaction。添加參數

1:保存片段的視圖的ID。 2:片段對象。 3:用於在堆棧中查找碎片的標籤。

這就是所有:)

享受你的片段!您還可以在添加或刪除動畫時設置動畫。

+0

非常感謝,回答 – AMH

+0

如何刪除所有的片段請 – AMH

+0

抱歉@AMH你可以用popBackStack(java.lang.String,int)從堆棧中刪除片段它的on http://developer.android.com/reference/android/app/FragmentManager.html文檔。 – Goofyahead

0

onCreateView()

當片段第一次繪製其用戶界面時,系統會調用它。要爲您的片段繪製UI,您必須從此方法返回一個視圖,該視圖是 片段佈局的根。如果片段沒有 提供UI,則可以返回null。

呼叫OnCreateView在你的片段類

return inflater.inflate(R.layout.fragmentlayout, container, false); 
0

我有類似的問題,當片段沒有出現。問題是我刪除了AndroidAnnotations,但忘記了將代碼從onViewCreated和onCreate方法移動到onCreateView。我通過比較兩個片段的代碼發現了這個錯誤 - 其中一個出現,另一個不出現。因此,如果您遇到類似問題,請嘗試檢查碎片代碼。它乍一看可能不會顯示任何錯誤,但由於這些錯誤而不會繼續工作或顯示。

特別是檢查你有這樣的方法和你誇大一個觀點:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    View v = inflater.inflate(R.layout.your_fragment_layout, container, false); 
    return v; 

}