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

  • 相关阅读:
    一些手打的笔记
    字符集的相关知识
    Java的概述以及语法
    Java和eclipxe的安装以及第一个程序
    制作Html标签以及表单、表格内容
    2,8,10,16进制之间的转换
    验证日期时间
    验证输入一个月的31天
    验证输入一年的12个月
    身份证号验证
  • 原文地址:https://www.cnblogs.com/wyx66688/p/10480306.html
Copyright © 2011-2022 走看看