标签:android开发
Android3.0开始引入这个新特性。主要用于实现一个多任务的界面。
fragement是activity的子界面,是运行在activity里面的。
步骤:
1、添加一个fragment一个类型,继承fragment;
2、在fragment中加载对应的布局文件;
3、得到fragment的管理器
4、得到fragment的事务管理器
5、在指定的容器中填充fragment
6、提交fragment的事务
代码:
//加载功能一对应的界面
//得到fragment的管理器
FragmentManager fm = this.getFragmentManager();
//得到fragment的事务管理器
FragmentTransaction ft = fm.beginTransaction();
Fragment01 f01 = new Fragment01();
//在指定的容器中填充fragment
ft.replace(R.id.fl_container, f01);
//提交fragment的事务
ft.commit();
把所有与fragment相关API都是从surpport.vx里面导入。
代码:
MainActivity.java:
package com.itheima.fragmentsy;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends FragmentActivity {
private FragmentManager fm;
private Fragment01 f01;
private EditText data;
private TextView tv_info;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data = (EditText) findViewById(R.id.data);
tv_info = (TextView) findViewById(R.id.tv_info);
fm = this.getSupportFragmentManager();
//得到fragment的事务管理器
FragmentTransaction ft = fm.beginTransaction();
f01 = new Fragment01();
//在指定的容器中填充fragment
ft.replace(R.id.fl_container, f01);
//提交fragment的事务
ft.commit();
}
public void submit(View view){
String dataStr = data.getText().toString();
f01.getData(dataStr);
}
public void open01(View view){
//加载功能一对应的界面
//得到fragment的事务管理器
FragmentTransaction ft = fm.beginTransaction();
//在指定的容器中填充fragment
ft.replace(R.id.fl_container, f01);
//提交fragment的事务
ft.commit();
}
public void open02(View view){
//加载功能一对应的界面
//得到fragment的事务管理器
FragmentTransaction ft = fm.beginTransaction();
Fragment02 f02 = new Fragment02();
//在指定的容器中填充fragment
ft.replace(R.id.fl_container, f02);
//提交fragment的事务
ft.commit();
}
public void open03(View view){
//加载功能一对应的界面
//得到fragment的事务管理器
FragmentTransaction ft = fm.beginTransaction();
Fragment03 f03 = new Fragment03();
//在指定的容器中填充fragment
ft.replace(R.id.fl_container, f03);
//提交fragment的事务
ft.commit();
}
public void getInfo(String info){
tv_info.setText(info);
}
}
Fragment01.java:
package com.itheima.fragmentsy;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Fragment01 extends Fragment {
private TextView tv_data;
private EditText et_info;
private Button bt_send;
/**
* 初始化fragment对象
* 加载布局文件
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.fragment01, null);
et_info = (EditText) view.findViewById(R.id.et_info);
//在对应的布局文件中的控件
tv_data = (TextView) view.findViewById(R.id.tv_data);
bt_send = (Button) view.findViewById(R.id.bt_send);
//给按钮添加单击事件的响应方法
bt_send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//把数据传递activity
send();
}
});
return view;
}
public void getData(String data){
tv_data.setText(data);
}
public void send(){
String info = et_info.getText().toString();
//得到运行fragment的activity
Activity a = this.getActivity();
//判断activity是不是mainactivity
if(a instanceof MainActivity){
MainActivity ma = (MainActivity)a;
//调用activity的方法,把数据传递过去
ma.getInfo(info);
}
}
}
步骤:
1、在fragment中提供了一个接收数据的方法;
2、在activity中调用fragment的方法,把数据传递过去;
代码:
package com.itheima.fragmentsy;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Fragment01 extends Fragment {
private TextView tv_data;
/**
* 初始化fragment对象
* 加载布局文件
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.fragment01, null);
//在对应的布局文件中的控件
tv_data = (TextView) view.findViewById(R.id.tv_data);
return view;
}
public void getData(String data){
tv_data.setText(data);
}
}
mainactivity中的方法:
public void submit(View view){
String dataStr = data.getText().toString();
f01.getData(dataStr);
}
在fragment中把数据传递给activity:
步骤:
1、在fragment得到目标activity;
2、调用activity的方法把数据传递过去;
代码:
package com.itheima.fragmentsy;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Fragment01 extends Fragment {
private TextView tv_data;
private EditText et_info;
private Button bt_send;
/**
* 初始化fragment对象
* 加载布局文件
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.fragment01, null);
et_info = (EditText) view.findViewById(R.id.et_info);
//在对应的布局文件中的控件
tv_data = (TextView) view.findViewById(R.id.tv_data);
bt_send = (Button) view.findViewById(R.id.bt_send);
//给按钮添加单击事件的响应方法
bt_send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//把数据传递activity
send();
}
});
return view;
}
public void send(){
String info = et_info.getText().toString();
//得到运行fragment的activity
Activity a = this.getActivity();
//判断activity是不是mainactivity
if(a instanceof MainActivity){
MainActivity ma = (MainActivity)a;
//调用activity的方法,把数据传递过去
ma.getInfo(info);
}
}
}
与activity生命周期的方法相比,多了:
onCreateView:加载布局文件
onDestroyView:销毁fragment的视图对象
01_为什么使用属性动画
02_属性动画入门
03_3种常见属性动画
代码:
package com.itheima.propertiesanimi;
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
}
public void trans(View view) {
// 把动画对象作用到按钮上
ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "translationX", 0, 10,
20, 40, 60, 100, 120, 140);
oa.setDuration(3000);
oa.setRepeatCount(2);
oa.setRepeatMode(ObjectAnimator.REVERSE);
oa.start();
}
public void rotate(View view) {
// 把动画对象作用到按钮上
ObjectAnimator ra = ObjectAnimator.ofFloat(iv, "rotation", 0, 30,
60, 90, 120, 150, 180);
ra.setDuration(3000);
ra.setRepeatCount(2);
ra.setRepeatMode(ObjectAnimator.REVERSE);
ra.start();
}
public void alpha(View view) {
// 把动画对象作用到按钮上
ObjectAnimator aa = ObjectAnimator.ofFloat(iv, "alpha", 0, 0.2f,
0.4f, 0.6f, 0.8f, 1.0f);
aa.setDuration(3000);
aa.setRepeatCount(2);
aa.setRepeatMode(ObjectAnimator.REVERSE);
aa.start();
}
public void scale(View view) {
// 把动画对象作用到按钮上
ObjectAnimator sa = ObjectAnimator.ofFloat(iv, "scaleY", 0, 0.2f,
0.4f, 0.6f, 0.8f, 1.0f);
sa.setDuration(3000);
sa.setRepeatCount(2);
sa.setRepeatMode(ObjectAnimator.REVERSE);
sa.start();
}
public void set(View view) {
AnimatorSet set = new AnimatorSet();
ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "translationX", 0, 10,
20, 40, 60, 100, 120, 140);
oa.setDuration(3000);
oa.setRepeatCount(2);
oa.setRepeatMode(ObjectAnimator.REVERSE);
ObjectAnimator ra = ObjectAnimator.ofFloat(iv, "rotation", 0, 30,
60, 90, 120, 150, 180);
ra.setDuration(3000);
ra.setRepeatCount(2);
ra.setRepeatMode(ObjectAnimator.REVERSE);
ObjectAnimator aa = ObjectAnimator.ofFloat(iv, "alpha", 0, 0.2f,
0.4f, 0.6f, 0.8f, 1.0f);
aa.setDuration(3000);
aa.setRepeatCount(2);
aa.setRepeatMode(ObjectAnimator.REVERSE);
// 把动画对象作用到按钮上
ObjectAnimator sa = ObjectAnimator.ofFloat(iv, "scaleY", 0, 0.2f,
0.4f, 0.6f, 0.8f, 1.0f);
sa.setDuration(3000);
sa.setRepeatCount(2);
sa.setRepeatMode(ObjectAnimator.REVERSE);
//组合播放动画
set.playTogether(oa,ra,aa,sa);
set.start();
}
}
样式:主要作用于控件上的,修饰控件的一些属性;
自定义样式:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="button_color_stype">
<item name="android:background" >#ff0000</item>
<item name="android:textColor">#00ff00</item>
</style>
<style name="textview_color_stype">
<item name="android:background" >#ff0000</item>
<item name="android:textColor">#0000ff</item>
</style>
<style name="textview_color_stype_largesize" parent="textview_color_stype">
<item name="android:textSize" >20sp</item>
<item name="android:background" >#0000ff</item>
</style>
</resources>
主题:界面或者整个应用程序的风格;
theme
定义主题的方法与定义样式完全一样;
代码:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="my_theme_activity_background">
<item name="android:background">#00ff00</item>
</style>
</resources>
<activity
android:name="com.itheima.style.MainActivity"
android:label="@string/app_name"
android:theme="@style/my_theme_activity_background"
>
标签:android开发
原文地址:http://blog.csdn.net/faith_yee/article/details/44853693