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方法进行动态的加载,只需传入动态加载碎片布局的类的对象参数。

  • 相关阅读:
    【贪心】POJ1017:Packets
    【贪心】POJ2393:Yogurt factory
    【贪心】POJ3190:Stall Reservations
    【递归】地盘划分
    【递归与递推】青蛙过河
    【搜索】POJ1242:Rescue
    单词方阵(dfs)
    反向因子Inverse Factorial
    P1604 B进制星球
    抵制克苏恩(记忆化搜索)
  • 原文地址:https://www.cnblogs.com/wyx66688/p/10480306.html
Copyright © 2011-2022 走看看