ps:学习安卓第五天。学习地点:http://www.jianshu.com/p/c3e80b913678
ps:其实把生命周期看完的时候,各种不懂。有的资料上说fragment是用来做平板的开发的,也有的 资料上直接把fragment当手机的界面跳转使用。所以判断起来就非常麻烦。
给我的fragment创建一个布局。可以理解为fragment 的外在显示
<?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" android:background="#00ff00"><TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="这里是fragment_first" android:layout_gravity="bottom" android:gravity="center" /></LinearLayout>布局创建好了之后,就要创建fragment的骨架了,并且要把我们的界面放在fragment中。
public class fragment_first extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v=inflater.inflate(R.layout.fragment_first,container,false); /* 第一个参数的含义为 view添加布局。 第二个参数的含义为 容器。 第三个参数的含义为 确定返回值,如果是false那就是返回了View如果是true的话就不一定返回true。 */ return v; }}之后就应该将我们的fragment嵌入到activity中去了。感觉在这个fragment更加像一个UI界面的工具和TextView类似的东西。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:orientation="vertical" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.hanlei.fragmenthl.MainActivity"> <TextView android:layout_width="match_parent" android:gravity="center" android:layout_height="100dp" android:text="这是activity" /> <fragment android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/fragment_First" android:name="com.example.hanlei.fragmenthl.fragment_first" /></LinearLayout>ps:感觉静态的fragment已经差不多了。可以继续学习了。突然就有一种学习使我快乐的感觉出来了
先给fragment建造界面
<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" tools:context="com.example.hanlei.fragmenthl1.BlankFragment"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="呈现另一个Fragment" android:id="@+id/btnShowAnotherFragment"/></LinearLayout>第二个fragment 的界面
<?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="match_parent" android:layout_height="wrap_content" android:text="这是另一个Fragment"/></LinearLayout>建造fragment
public class BlankFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) { View v=inflater.inflate(R.layout.fragment_blank,container,false); v.findViewById(R.id.btnShowAnotherFragment).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getFragmentManager().beginTransaction().replace(R.id.activity_main,new AnotherFragment()).commit();//跳转到另一个fragment } }); return v; }另一个fragment建造
public class AnotherFragment extends android.support.v4.app.Fragment{ @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v=inflater.inflate(R.layout.fragment_another,container,false); return v; }}Activity跳转fragment
public class MainActivity extends AppCompatActivity { @Override PRotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState==null){ getSupportFragmentManager().beginTransaction() .add(R.id.activity_main,new BlankFragment()).commit(); } }}ps:动态fragment的实例是来自极客学院。
新闻热点
疑难解答