2016-01-16 153 views
0

我想在片段的活動中顯示條形圖,但它顯示的是膨脹問題。所以,我改變了我的活動到FragmentActivity它工作我的應用程序正在運行,但Fragment部分輸出不顯示。活動和片段活動的片段不起作用

這裏是我的代碼我無法找出什麼錯誤,我已經做了:

MainActivity:

public class MainActivity extends Activity { 


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

activity_main.xml中:

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

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello_world" /> 
    <fragment 
      android:id="@+id/fghis" 
      android:layout_width="match_parent" 
      android:layout_height="100dp" 
      class="com.anu.actualdemobargraph.HistoryFrag" /> 
</LinearLayout> 

HistoryFrag:

import java.util.ArrayList; 

import android.graphics.Color; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

import com.echo.holographlibrary.Bar; 
import com.echo.holographlibrary.BarGraph; 
import com.echo.holographlibrary.BarGraph.OnBarClickedListener; 

public class HistoryFrag extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.historyfrag, container,false); 
     BarGraph g = (BarGraph)view.findViewById(R.id.bargraph); 
     ArrayList<Bar> points = new ArrayList<Bar>(); 
     Bar d = new Bar(); 
     d.setColor(Color.parseColor("#c6c6c6")); 
     d.setName("Test1"); 
     d.setValue(10); 
     Bar d2 = new Bar(); 
     d2.setColor(Color.parseColor("#c6c6c6")); 
     d2.setName("Test2"); 
     d2.setValue(20); 
     Bar d4 = new Bar(); 
     d4.setColor(Color.parseColor("#c6c6c6")); 
     d4.setName("Test2"); 
     d4.setValue(20); 
     Bar d5 = new Bar(); 
     d5.setColor(Color.parseColor("#FFBB33")); 
     d5.setName("Test2"); 
     d5.setValue(20); 
     Bar d6 = new Bar(); 
     d6.setColor(Color.parseColor("#c6c6c6")); 
     d6.setName("Test2"); 
     d6.setValue(20); 
     Bar d7 = new Bar(); 
     d7.setColor(Color.parseColor("#c6c6c6")); 
     d7.setName("Test2"); 
     d7.setValue(20); 
     Bar d8 = new Bar(); 
     d8.setColor(Color.parseColor("#c6c6c6")); 
     d8.setName("Test2"); 
     d8.setValue(20); 
     Bar d3 = new Bar(); 
     d3.setColor(Color.parseColor("#c6c6c6")); 
     d3.setName("Test3"); 
     d3.setValue(50); 
//  d3.setStackedBar(true); 
//  d3.AddStackValue(new BarStackSegment(2, Color.parseColor("#FFBB33"))); 
//  d3.AddStackValue(new BarStackSegment(4, Color.RED)); 
     points.add(d); 
     points.add(d2); 
     points.add(d3); 
     points.add(d4); 
     points.add(d5); 
     points.add(d6); 
     points.add(d7); 
     points.add(d8); 


     g.setUnit("meters"); 
     g.appendUnit(true); 
     g.setBars(points); 

     g.setOnBarClickedListener(new OnBarClickedListener(){ 

      @Override 
      public void onClick(int index) { 

      } 

     }); 
     return view; 
    } 
    public HistoryFrag() { 
     // TODO Auto-generated constructor stub 
    } 
} 

historyfrag.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <com.echo.holographlibrary.BarGraph 
     android:id="@+id/bargraph" 
     android:layout_width="match_parent" 
     android:layout_height="200dp" /> 

</LinearLayout> 

在historyfrag.xml,條形圖從庫中提取的存在庫中沒有問題。

在此先感謝。

回答

0

您需要使用FragmentManager將片段附加到您的活動。取決於你想使用框架片段還是支持片段,這個過程略有不同。

框架片段

這是內置於Android框架,並在API 11和上述可用的片段類。

要使用的片段類,這是你需要在你的HistoryFrag類改變import語句Android框架的一部分:

import android.support.v4.app.Fragment; 

import android.app.Fragment; 

然後在您的活動onCreate方法您需要添加:

getFragmentManager() 
    .beginTransaction() 
    .replace(R.id.fghis, new HistoryFrag()) 
    .commit(); 

支持庫文件片段

如果您要支持API級別< 11,那麼您需要使用Support Library中的Fragment類。這將將片段功能支持API 4.

如果要使用支持庫中的片段類,則需要確保擴展了FragmentActivity並將HistoryFrag類中的導入語句保留爲android.support.v4.app.Fragment

然後在您的活動onCreate()添加方法是這樣的:

getSupportFragmentManager() 
    .beginTransaction() 
    .replace(R.id.fghis, new HistoryFrag()) 
    .commit(); 

那麼這將碎片連接到活動。

+0

嗨Jahnold, 其實在我的情況下,我不想使用FragmentActivity。那麼,我不能在Activity中使用這個東西嗎? –

+0

你可以,你將不得不使用框架片段而不是支持。我會在答案中添加詳細信息 – Jahnold