Fragment 模仿QQ页面之间的转换
16lz
2021-01-26
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.frangment.MainActivity" > <FrameLayout android:id="@+id/fff" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="9" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal" > <TextView android:id="@+id/msg" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="消息" android:textSize="15sp" /> <TextView android:id="@+id/contact" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="联系人" android:textSize="15sp" /> <TextView android:id="@+id/activity" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="动态" android:textSize="15sp" /> </LinearLayout></LinearLayout>
package com.example.frangment;import android.app.Activity;import android.app.Fragment;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.graphics.Color;import android.os.Bundle;import android.view.View;import android.widget.TextView;public class MainActivity extends Activity {private TextView message,contact,activity; private Fragmentmsg fsg;private Fragmentcontact fc;private Fragmentactivity fct;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);message = (TextView) findViewById(R.id.msg);contact = (TextView) findViewById(R.id.contact);activity = (TextView) findViewById(R.id.activity);fsg=new Fragmentmsg();fc=new Fragmentcontact();fct=new Fragmentactivity();choice(0);message.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {choice(0);}});contact.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {choice(1);}});activity.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {choice(2);}});}private void choice(int i) {if (i == 0) {message.setTextColor(Color.BLUE);message.setBackgroundColor(Color.GREEN);contact.setTextColor(Color.LTGRAY);contact.setBackgroundColor(Color.WHITE);activity.setTextColor(Color.LTGRAY);activity.setBackgroundColor(Color.WHITE);loadfragement(fsg);}if (i == 1) {contact.setTextColor(Color.BLUE);contact.setBackgroundColor(Color.GREEN);message.setTextColor(Color.LTGRAY);message.setBackgroundColor(Color.WHITE);activity.setTextColor(Color.LTGRAY);activity.setBackgroundColor(Color.WHITE);loadfragement(fc);}if (i == 2) {activity.setTextColor(Color.BLUE);activity.setBackgroundColor(Color.GREEN);contact.setTextColor(Color.LTGRAY);contact.setBackgroundColor(Color.WHITE);message.setTextColor(Color.LTGRAY);message.setBackgroundColor(Color.WHITE);loadfragement(fct);}}private void loadfragement(Fragment f){FragmentManager fm=this.getFragmentManager();FragmentTransaction ft=fm.beginTransaction();ft.replace(R.id.fff, f);ft.commit();}}
更多相关文章
- listview中CheckBox的监听回调
- Android(安卓)自动向手机添加联系人(测试数据用)
- Android(安卓)动态设置布局文件的exception
- 动态生成控件
- android 实现ListView中添加RaidoButton单选
- 【黑马Android】(03)学生管理系统/动态刷新界面
- android之Handler控制进度条
- HandlerTest
- Aandroid 关于处理个推透传消息