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>

运行效果:


posted on 2018-05-05 23:15  观心静 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/guanxinjing/p/9708627.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值