2013-02-28 117 views
0

我是碎片的新手,我正在努力工作。以下是我的代碼。爲了它。當我運行我的應用程序沒有任何錯誤消息,但我沒有收到輸出片段不顯示預期輸出

MainActivity.java

public class MainActivity extends FragmentActivity{ 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

} 

} 

MyFragments.java

public class MyFragments extends Fragment { 

public MyFragments() { 
    // TODO Auto-generated constructor stub 
} 

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

fragment_one.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/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="TextView" /> 

</LinearLayout> 

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/LinearLayout1" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
tools:context=".MainActivity" > 

<fragment 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/fragment_One" 
    android:name="com.benchmark.fragmentsdemo.MyFragments" 
    android:layout_width="0dp" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" /> 

<fragment 
    android:id="@+id/fragment_two" 
    android:name="com.benchmark.fragmentsdemo.MyFragments" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="2" /> 

</LinearLayout> 

請人幫助我

+0

你的意思是你的碎片沒有顯示? – FWeigl 2013-02-28 09:00:16

回答

0

您的佈局不正確實施。

請嘗試以下佈局。

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/LinearLayout1" 
android:layout_width="match_parent" 
android:weightSum="1" 
android:layout_height="match_parent" 
android:orientation="vertical" 
tools:context=".MainActivity" > 

<fragment 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/fragment_One" 
    android:name="com.benchmark.fragmentsdemo.MyFragments" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.5" /> 

<fragment 
    android:id="@+id/fragment_two" 
    android:name="com.benchmark.fragmentsdemo.MyFragments" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.5" /> 

</LinearLayout> 

父的LinearLayout的方向是垂直和你正在作出片段0dp的 寬度,而不是高度

+0

謝謝。問題解決了 – Amrut 2013-02-28 09:44:10

0

試試這個:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/LinearLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     tools:context=".MainActivity" > 
0

嘿檢查出本教程的片段存在一步一步的程序來創建它,所以它可能對你有用Click here並且還檢查documentation w這個由developer.android提供。