码迷,mamicode.com
首页 > 其他好文 > 详细

替代ActivityGroup的Fragment,实现主界面

时间:2014-09-24 19:40:17      阅读:293      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   color   io   os   使用   ar   

Fragment低版本使用需要导入V4包

主类文件:(因为低版本Activity中无法调用getFragmentManager()方法,所以继承FragmentActivity)

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
import com.example.testfragment.fragment.FragmentOne;
import com.example.testfragment.fragment.FragmentTwo;

public class FragmentActivityTest extends FragmentActivity {
    public String test = "test";
    public Button button1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_activity);
        getWidget();
    }
    //获取控件并添加单击事件
    private void getWidget() {
        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentOne fragmentOne = new FragmentOne();
                getSupportFragmentManager().beginTransaction().replace(R.id.framelayout, fragmentOne).commit();
            }
        });
        Button button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTwo fragmentOne = new FragmentTwo();
                getSupportFragmentManager().beginTransaction().replace(R.id.framelayout, fragmentOne).commit();
            }
        });
    }
}

fragment_activity布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    <RelativeLayout android:layout_width="match_parent"
                    android:layout_height="48dp"
                    android:background="#ff201bff"
                    android:id="@+id/top_layout"></RelativeLayout>

    <RelativeLayout android:layout_width="match_parent"
                    android:layout_height="48dp"
                    android:background="#ff201bff"
                    android:id="@+id/bottom_layout1"
                    android:layout_alignParentBottom="true"
            >
        <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/button1"
                android:text="界面1"
                android:layout_alignParentLeft="true"/>
        <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/button2"
                android:layout_alignParentRight="true"
                android:text="界面2"/>
    </RelativeLayout>
    <FrameLayout android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:id="@+id/framelayout"
                 android:layout_below="@id/top_layout"
                 android:layout_above="@id/bottom_layout1"
            />
</RelativeLayout>

 

两个Fragment文件以及简单布局:

1、

在Fragment 中可以利用方法getActivity()获取所依托的activity,然后即可获取activity中的数据

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.testfragment.FragmentActivityTest;
import com.example.testfragment.R;

public class FragmentOne extends Fragment {
    private FragmentActivityTest activity;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_one, container, false);
        return view;
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        activity = (FragmentActivityTest) getActivity();
        activity.button1.setText(activity.test);
    }
}

R.layout.fragment_one布局文件:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="界面1"
            android:id="@+id/textView" android:layout_gravity="center_horizontal"/>
</LinearLayout>

2、

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.testfragment.R;

public class FragmentTwo extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_two, container, false);
        return view;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="界面2"
            android:id="@+id/textView" android:layout_gravity="center_horizontal"/>
</LinearLayout>

替代ActivityGroup的Fragment,实现主界面

标签:android   style   blog   http   color   io   os   使用   ar   

原文地址:http://www.cnblogs.com/ccddy/p/3990963.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!