FragmentApp界面

本文展示了如何使用Android的Fragment实现界面跳转,并通过示例代码详细介绍了实现过程,包括不同界面间的颜色变化及跳转逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一般界面跳转,效果图如下:




实现代码:

package com.example.fragmentapp;

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 messageText, contactText, startText;
	private MessageFragment messageFragment;
	private ContactFragment contactFragment;
	private StartFragment startFragment;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		messageText = (TextView) findViewById(R.id.message);
		contactText = (TextView) findViewById(R.id.contact);
		startText = (TextView) findViewById(R.id.start);

		messageFragment = new MessageFragment();
		contactFragment = new ContactFragment();
		startFragment = new StartFragment();

		choose(0);

		messageText.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				choose(0);
			}
		});

		contactText.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				choose(1);
			}
		});

		startText.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				choose(2);
			}
		});
	}

	private void choose(int pos) {
		if (pos == 0) {
			messageText.setTextColor(Color.RED);
			messageText.setBackgroundColor(Color.LTGRAY);

			contactText.setTextColor(Color.DKGRAY);
			contactText.setBackgroundColor(Color.WHITE);

			startText.setTextColor(Color.DKGRAY);
			startText.setBackgroundColor(Color.WHITE);

			loadFragment(messageFragment);

		}
		if (pos == 1) {
			contactText.setTextColor(Color.RED);
			contactText.setBackgroundColor(Color.LTGRAY);

			messageText.setTextColor(Color.DKGRAY);
			messageText.setBackgroundColor(Color.WHITE);

			startText.setTextColor(Color.DKGRAY);
			startText.setBackgroundColor(Color.WHITE);

			loadFragment(contactFragment);
		}

		if (pos == 2) {
			startText.setTextColor(Color.RED);
			startText.setBackgroundColor(Color.LTGRAY);

			messageText.setTextColor(Color.DKGRAY);
			messageText.setBackgroundColor(Color.WHITE);

			contactText.setTextColor(Color.DKGRAY);
			contactText.setBackgroundColor(Color.WHITE);

			loadFragment(startFragment);

		}
	}

	private void loadFragment(Fragment f) {

		FragmentManager fm = this.getFragmentManager();
		FragmentTransaction ft = fm.beginTransaction();

		ft.replace(R.id.content, f);
		ft.commit();

	}
}

package com.example.fragmentapp;

import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MessageFragment extends Fragment{

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		View view = inflater.inflate(android.R.layout.simple_list_item_1,null);
		TextView text = (TextView) view.findViewById(android.R.id.text1);
		text.setText("消息界面");
		text.setBackgroundColor(Color.YELLOW);
		
		
		return view;
	}
	

}

package com.example.fragmentapp;

import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ContactFragment extends Fragment{
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		View view=inflater.inflate(android.R.layout.simple_list_item_1,	null);
		TextView text=(TextView) view.findViewById(android.R.id.text1);
		text.setText("联系人界面");
		text.setBackgroundColor(Color.GREEN);
		
		return	view;
	}

}

package com.example.fragmentapp;

import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class StartFragment extends Fragment {
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		View view=inflater.inflate(android.R.layout.simple_list_item_1,	null);
		TextView text=(TextView) view.findViewById(android.R.id.text1);
		text.setText("动态界面");
		text.setBackgroundColor(Color.RED);
		
		return	view;
	}

}

xml布局:

<LinearLayout 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"
    android:orientation="vertical"
    tools:context="com.example.fragmentapp.MainActivity" >

   <FrameLayout
        android:id="@+id/content"
        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/message"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:textSize="25sp"
            android:text="消息" />

        <TextView
            android:id="@+id/contact"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:textSize="25sp"
            android:text="联系人" />
        
        <TextView
            android:id="@+id/start"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:textSize="25sp"
            android:text="动态" />
      
    </LinearLayout>

</LinearLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值