0

In my app initially I created one class "abc.java" which extends Activity and do some functionality..

But now I have decided to implement TabView and abc.java should be first tab in tabview..

My problem is my abc.java class extends activity and now when I change it to fragments , it gives me errors.

If I implement TabActivity , which is deprecated now, it will work fine, but now if I want to use fragment what changes I have to do?

I know I have to make some changes in abc.java but I have no clue how to do that..I am new to android and trying hard to get this done..Any help would be great!!

here is my code for class "abc.java"

public class CountAndMarkPage extends Activity
{
  public void onCreate(Bundle savedInstanceState)
  {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.count_mark);

        //lots of code here
   }
 }
4

1 回答 1

0

您需要使用相同的布局资源扩展 Fragment 并覆盖 onCreateView。从文档中查看此信息。

快速示例:

public class CountAndMarkPage extends Fragment {
    public void onCreate(Bundle savedInstanceState){
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState){
        // Inflate your layout
        View myView = inflater.inflate(R.layout.count_mark, container, false);

        // Any additional initialization

        return myView;
    }
}

要访问此布局中的小部件,您需要像这样在膨胀的根视图上使用 findViewById

public class CountAndMarkPage extends Fragment {
    public void onCreate(Bundle savedInstanceState){
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState){
        // Inflate your layout
        ViewGroup myView = (ViewGroup) inflater.inflate(R.layout.count_mark, container, false);

        // Any additional initialization
        TextView myTextView = (TextView) myView.findViewById(R.id.my_view);
        // And so on
        return myView;
    }
}    
于 2012-09-18T20:12:53.873 回答