FragmentApp界面

51 篇文章 0 订阅

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




实现代码:

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>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 创建一个新的Fragment类。 2. 在activity中添加一个FrameLayout,作为容器用于显示fragment。 3. 在activity中使用FragmentManager来管理fragment。 4. 在activity中使用FragmentTransaction来添加、删除、替换和执行其他fragment操作。 5. 在activity中使用commit()方法来提交fragment事务。 以下是一个示例代码: 在MainActivity.java中: ``` public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 创建Fragment实例 MyFragment myFragment = new MyFragment(); // 获取FragmentManager FragmentManager fragmentManager = getSupportFragmentManager(); // 开始FragmentTransaction FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); // 将Fragment添加到容器中 fragmentTransaction.add(R.id.container, myFragment); // 提交事务 fragmentTransaction.commit(); } } ``` 在MyFragment.java中: ``` public class MyFragment extends Fragment { @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { // 加载布局文件 View view = inflater.inflate(R.layout.fragment_my, container, false); return view; } } ``` 在activity_main.xml中: ``` <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 在fragment_my.xml中: ``` <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="wrap_content" android:layout_height="wrap_content" android:text="这是一个Fragment界面" /> </LinearLayout> ``` 运行app后,就可以在MainActivity中看到一个包含“这是一个Fragment界面”的Fragment界面
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值