随笔 - 632  文章 - 17  评论 - 54  阅读 - 93万

Android Fragment(二)

废话:在上一篇的博客中我们给出了Fragment的简单介绍,这一片博客给大家介绍一下Fragment到底该怎样用。主要都用在哪方面等等。

需求:现有一个界面,要求,竖屏时界面的背景颜色为红色,横屏时界面的的背景颜色为黄色。(主要目的是为了给大家演示一下Fragment实现动态UI效果)

直接看代码好了:

一、背景颜色为红色的Fragment

复制代码
package com.yw.myapiupdate.fragment;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yw.myapiupdate.R;

@SuppressLint("NewApi")
public class FragmentRed extends Fragment{
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragmentred, container,false);
    }
}
复制代码

红色布局:

复制代码
<?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" >
    
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ff0000"
    android:text="这是一个fragment的测试程序"/>
</LinearLayout>
复制代码

二、背景颜色为黄色的Fragment

 

复制代码
package com.yw.myapiupdate.fragment;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yw.myapiupdate.R;

@SuppressLint("NewApi")
public class FragmentYellow extends Fragment{
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragmentyellow, container,false);
    }
}
复制代码

黄色布局:

复制代码
<?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" >
    <TextView 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="这是一个fragment的测试程序"
    android:background="#ffff00"/>

</LinearLayout>
复制代码

三、承载这两个布局的Activity

复制代码
package com.yw.myapiupdate.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.Display;

import com.yw.myapiupdate.R;

@SuppressLint("NewApi")
public class MainActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_layout);
        Display display = getWindowManager().getDefaultDisplay();
        if(display.getWidth() > display.getHeight()){
            FragmentRed red = new FragmentRed();
            getFragmentManager().beginTransaction().replace(R.id.fragment_linear_layout, red).commit();
        }else{
            FragmentYellow yellow = new FragmentYellow();
            getFragmentManager().beginTransaction().replace(R.id.fragment_linear_layout, yellow).commit();
        }
    }
}
复制代码

主文件布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_linear_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" 
    android:baselineAligned="false">
</LinearLayout>

 

 

posted on   飘杨......  阅读(455)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示