zoukankan      html  css  js  c++  java
  • Android中碎片的添加问题

    碎片在Android中的应用是十分广泛的,它就像是嵌在活动中的另一个活动就像是一个容器包含了另一个容器,那么到底该怎么添加碎片呢?主要有两种方法,一种是在该碎片所在的xml文档中使用Android:name属性来添加动态加载碎片布局的类的类名(包括完整的包名),动态加载碎片布局用的就是layoutinflater类中的inflate方法,

    package com.example.asus.fragmenttest;
    
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    /**
     * Created by ASUS on 2019/2/23.
     */
    
    public class AnotherRightFragment extends Fragment {
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view=inflater.inflate(R.layout.another_right_fragment,container,false);
            return view;
        }
    }
    View Code

    而碎片实质上和活动一样,它也有xml布局文档等等之类的活动有的东西。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ffff00">
    <TextView
        android:text="This is another right fragment"
        android:layout_gravity="center_horizontal"
        android:textSize="20sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    </LinearLayout>
    View Code

    碎片只是放在了<frgment>控件中,但是它五脏俱全

    如果不把碎片放在<frgment>控件中,那么可以再活动的.java文件中用replacefragment方法进行动态的加载,只需传入动态加载碎片布局的类的对象参数。

  • 相关阅读:
    C++成员变量与函数内存分配
    Sqlite ContentProvider Loader 上下文 对话框
    好书好人生--读书的步骤
    小智慧40
    流媒体开发之-直播界面切换电视台频道
    HDU 4617Weapon(两条异面直线的距离)
    BON取代半岛电视,美国人要“换口味”了吗?
    【Todo】Lucene系统学习
    Zookeeper学习 & Paxos
    C++中的虚继承 & 重载隐藏覆盖的讨论
  • 原文地址:https://www.cnblogs.com/wyx66688/p/10480306.html
Copyright © 2011-2022 走看看