Fragment

只需了解即可
使用AbsoluteLayout时,每个子控件都可指定两个属性:
layout_x:指定该子组件的X坐标
layout_y:指定该子组件的Y坐标


Button_Selector

#

Button_Seletor是定义在Drawble下的按钮选择器
指的是选中按钮时的一些信息,所以应该设为true。
指的是按下按钮的变化
相应的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--指定按钮按下时的颜色-->
   <item android:state_pressed="true"
       android:state_enabled="true"
       android:drawable="@color/colorAccent"/>
    <item android:drawable="@color/colorPlu"/>
</selector>

activity_main.xml布局如下:

<?xml version="1.0" encoding="utf-8"?>
<!--定义两个Button,点击第一个button显示对应的代码-->
<!--最外层是一个Relativelayout布局-->
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.myfragment.MainActivity">
    <!--紧接着定义一个FrameLayout,即帧布局.
    让他的高度不要填充整个容器.让他位于LinearLayout的上方-->
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/framelayout"
        android:layout_above="@+id/line"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/line"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:weightSum="2"
        android:layout_alignParentLeft="true">
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="第一个页面"
            android:textColor="#000"
            android:background="@drawable/button_selector"/>

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="第二个页面"
            android:textColor="#f0f"/>


    </LinearLayout>

</RelativeLayout>

Fragment即所谓的碎片模式,如果要写碎片控件则通过android:name属性来显示指明要添加的碎片类名,一定要将类的包名加上。

package com.example.myfragment;

import android.support.annotation.AnimRes;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.annotation.StyleRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.FrameLayout;

import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    //注意:FragmentTransaction不能定义为全局变量
    private Button mButton1,mButton2;
    private FragmentManager  manager;
    private FrameLayout framelayout;
    private FragmentOne fragmentOne;
    private FragmentTwo fragmentTwo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButton1= (Button) findViewById(R.id.btn1);
        mButton2= (Button) findViewById(R.id.btn2);
        mButton1.setOnClickListener(this);
        mButton2.setOnClickListener(this);

        manager=getSupportFragmentManager();
        framelayout= (FrameLayout) findViewById(R.id.framelayout);
        FragmentTransaction transaction=manager.beginTransaction();
        fragmentOne=new FragmentOne();
        //必须使用commit方法进行提交
        transaction.add(R.id.framelayout, fragmentOne).commit();



  }

    @Override
    public void onClick(View v) {
     FragmentTransaction transaction=manager.beginTransaction();
        switch (v.getId()){
            case R.id.btn1:
                fragmentTwo=new FragmentTwo();
                transaction.replace(R.id.framelayout,fragmentTwo);
                transaction.addToBackStack(null);
                transaction.commit();
                break;
            case R.id.btn2:
                transaction.replace(R.id.framelayout,fragmentTwo);
                transaction.addToBackStack(null).commit();
                break;
        }

    }
}
package com.example.myfragment;

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;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 杜春慧 on 2016/4/19.
 */
public class FragmentOne extends Fragment {
    //定义一个Fragment类继承与Fragment,注意:导包时一定是v4包下的
    //定义一个集合,用来存放数据
    private List<String> list;
    //该类对应的布局中只定义了一个控件
    private ListView listView;
    //先写一个FragmentOne类,对应的将布局加载进来
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragmentone,null);
        listView= (ListView) view.findViewById(R.id.fragmentone);
        list=new ArrayList<>();
        list.add("所有的结局都已写好");
        list.add("所有的泪水也都启程");
        list.add("却忽然忘了是怎样的一个开始");
        list.add("年轻的你只如云影掠过");
        list.add("而你微笑的面容极浅极淡");
        list.add("逐渐隐没在日落后后的群岚");
        list.add("如果我有让时间倒转的魔力");
        list.add("我永远都不会使用它");
        list.add("那样的话,你经历的每一个时刻都变得毫无意义");
        list.add("因为你总是可以回去做一次");
        list.add("失去了它的味道");
        list.add("失去了它的美");
        //这里将ArrayAdapter实例化,注意构造方法中有三个参数,了解每个参数的含义
        //第一个参数不能用this,第二个参数是Android系统自带的布局,第三个是集合元素
        ArrayAdapter adapter=new ArrayAdapter(getActivity(),android.R.layout.simple_expandable_list_item_1,list);
        //将adapter里的内容放到列表ListView中
        listView.setAdapter(adapter);
        return view;
    }
}
package com.example.myfragment;


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;
import android.widget.ImageView;


/**
 * Created by 杜春慧 on 2016/4/19.
 */
public class FragmentTwo extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragmenettwo,null);
        ImageView imageView= (ImageView) view.findViewById(R.id.fragmenttwo);
        imageView.setImageResource(R.mipmap.kemi);
        return view;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值