zoukankan      html  css  js  c++  java
  • Fragment的创建与使用

    使用Fragment最简单的方式就是把Fragment当成最普通的控件,可以直接写在Activity的布局文件中。整个过程只需要两步:一是继承Fragment,重写onCreateView,决定Fragment的布局,二是在Activity布局文件中加入Fragment,与普通的View一样。
    MainActivity.java

    package com.sdutacm.viewpager.Fragment;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    
    import com.sdutacm.viewpager.R;
    
    public class Main2Activity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    }
    

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        android:orientation="vertical"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.sdutacm.viewpager.Fragment.MainActivity">
    
        <fragment
            android:id="@+id/id_fragment_title"
            android:name="com.sdutacm.viewpager.Fragment.TitleFragment"
            android:layout_width="match_parent"
            android:layout_height="45dp" />
    
        <fragment
            android:id="@+id/id_fragment_content"
            android:name="com.sdutacm.viewpager.Fragment.ContentFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </LinearLayout>
    

    TitleFragment.java

    package com.sdutacm.viewpager.Fragment;
    
    import android.app.Fragment;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    import com.sdutacm.viewpager.R;
    
    /**
     * Created by bummer on 2017/8/14.
     */
    
    public class TitleFragment extends Fragment {
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_title,container,false);
            return view;
        }
    }
    

    fragment_title.xml

    <?xml version="1.0" encoding="utf-8"?>
    <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:background="#EE30A7"
        tools:context="com.sdutacm.viewpager.Fragment.TitleFragment">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="新闻浏览器"
            android:textSize="28sp" />
    
    </LinearLayout>

    ContentFragment.java

    package com.sdutacm.viewpager.Fragment;
    
    import android.app.Fragment;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.TextView;
    
    import com.sdutacm.viewpager.R;
    
    /**
     * Created by bummer on 2017/8/14.
     */
    
    public class ContentFragment extends Fragment{
        private Button contentButton;
        private TextView textView;
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_content,container,false);
            intiView(view);
            return view;
        }
    
        private void intiView(View view) {
            contentButton = (Button) view.findViewById(R.id.contentButton);
            textView = (TextView) view.findViewById(R.id.contentText);
            contentButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    textView.setText("中国羽毛队获得了奥运会冠军");
                }
            });
        }
    }
    

    fragment_content.xml

    <?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"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context="com.sdutacm.viewpager.Fragment.ContentFragment"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:id="@+id/contentText"
            android:text="@string/content"
            android:textSize="25sp"
            />
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/contentButton"
            android:text="@string/addNews"
            />
    
    </LinearLayout>
  • 相关阅读:
    IIS配置跨域请求
    ABP框架页面权限验证
    WPF-DataGrid增删改查不绑定数据源
    WPF-DataGrid增删改查绑定数据源
    .NET开发框架集合(长期更新)
    C# Webbrowser 常用方法及多线程调用
    Devexpress-GridLookUpEdit
    Devexpress提示框的使用
    Asp.Net MVC中Action跳转小结
    ASP.NET MVC备忘
  • 原文地址:https://www.cnblogs.com/CCCrunner/p/11781842.html
Copyright © 2011-2022 走看看