zoukankan      html  css  js  c++  java
  • android开发 静态碎片布局实现

    实现思维:

    1.需要写2个或者多个子布局

    2.写一个Java的class去实现将子布局与父类布局铺满。(一个子布局对应一个class)

    3.在父类布局中导入fragment布局,并且添加android:name=“”属性;

    1.写2个布局:

    <?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="@color/colorAccent">
        <TextView
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="下面的布局"
            android:textSize="30sp"
            android:textColor="@color/colorBlack"
            android:maxEms="1"
            android:maxLines="5"
            android:layout_gravity="center"
            />
    
    </LinearLayout>
     
    <?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_marginTop="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="上面的布局"
            android:textSize="30sp"
            android:textColor="@color/colorBlack"
            android:maxEms="1"
            android:maxLines="5"/>
    </LinearLayout>
     

    2.Java的class去实现将子布局与父类布局铺满

    package com.example.lenovo.mydebrisdemo;
    
    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;
    
    /**
     * Created by lenovo on 2018/5/5.
     */
    
    public class Top extends Fragment {
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.top_fragment,container,false);
            return view;
        }
    }
     
    package com.example.lenovo.mydebrisdemo;
    
    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;
    
    /**
     * Created by lenovo on 2018/5/5.
     */
    
    public class Button extends Fragment {
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.botton_fragment,container,false);
            return view;
        }
    }

    3.父类布局中导入fragment布局,并且添加android:name=“”属性;

    <?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">
        <!--id一定要加否则会报错-->
        <fragment
            android:id="@+id/top"
            android:name="com.example.lenovo.mydebrisdemo.Top"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>
        <fragment
            android:id="@+id/button"
            android:name="com.example.lenovo.mydebrisdemo.Button"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>
    
    </LinearLayout>

    运行效果:

  • 相关阅读:
    MongoDB配置及解答mongodb cmd中安装及连接数据库 mongodb无法安装及连接数据库解答
    类封装版学生管理系统,连接数据库,增删改查,拿去用,不谢。
    python学生管理系统连接数据库版,很详细,这个是用函数版的增删改查,拿去用,不谢。
    京东双十一
    Ubuntu中以root权限运行软件sudo命令不输入密码
    Ubuntu 18.04中sudo运行的程序无法切换输入法中文输入问题
    UVA12558 埃及分数
    NOI1999 生日蛋糕
    Tarjan
    AC自动机
  • 原文地址:https://www.cnblogs.com/guanxinjing/p/9708627.html
Copyright © 2011-2022 走看看