Android之Fragment小案例

您所在的位置:网站首页 android怎么设置按钮颜色 Android之Fragment小案例

Android之Fragment小案例

2024-01-17 11:24| 来源: 网络整理| 查看: 265

Fragment小案例——按钮切换背景颜色效果 fragment_text(Activity)fragment_text_layout.xmlfragment_text_layout2.xmlfragment_text1.javafragment_text_layout3.xmlfragment_text2.javafragment_text_layout4.xmlfragment_text3.javafragment_text_layout5.xml最终实现效果

fragment_text(Activity)

fragment_text(Activity)

package com.example.myapplication_one; import android.annotation.SuppressLint; 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.Button; public class fragment_text extends AppCompatActivity implements View.OnClickListener { Button button1,button2,button3,button4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_text_layout); init(); button1.setOnClickListener(this); button2.setOnClickListener(this); button3.setOnClickListener(this); button4.setOnClickListener(this); fragment_text1 fragment_text1 = new fragment_text1(); /* * 1、获取管理者 * 2、开启事务 * 3、实现功能:replace * 4、提交 */ FragmentManager fragmentManager = getSupportFragmentManager(); android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.fragment_layout, fragment_text1); //fragmentTransaction.show(); fragmentTransaction.commit(); } private void init(){ button1 = findViewById(R.id.button1); button2 = findViewById(R.id.button2); button3 = findViewById(R.id.button3); button4 = findViewById(R.id.button4); } @SuppressLint("ResourceType") public void onClick(View view){ FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); switch (view.getId()){ case R.id.button1://显示第一个Fragemnt fragmentTransaction.replace(R.id.fragment_layout,new fragment_text2()); break; case R.id.button2://显示第2个Fragemnt fragmentTransaction.replace(R.id.fragment_layout,new fragment_text3()); break; // case R.id.button3://显示第3个Fragemnt // fragmentTransaction.replace(R.layout.fragment_text_layout4,new fragment_text2()); // break; // case R.id.button4://显示第4个Fragemnt // fragmentTransaction.replace(R.layout.fragment_text_layout4,new fragment_text2()); // break; // default: // break; } fragmentTransaction.addToBackStack(null);//返回栈 fragmentTransaction.commit(); } } fragment_text_layout.xml

fragment_text_layout.xml

效果:

注意:include的引用,用于显示一个新的页面布局。

fragment_text_layout2.xml

fragment_text_layout2.xml

效果:

fragment_text1.java

fragment_text1.java

package com.example.myapplication_one; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class fragment_text1 extends Fragment { @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_text_layout3,container,false); // View view2 = inflater.inflate(R.layout.activity_list_item,null); return view; } } fragment_text_layout3.xml

fragment_text_layout3.xml

效果:

fragment_text2.java

fragment_text2.java

package com.example.myapplication_one; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class fragment_text2 extends Fragment { @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_text_layout4,container,false); // View view2 = inflater.inflate(R.layout.activity_list_item,null); return view; } } fragment_text_layout4.xml

fragment_text_layout4.xml

效果:

fragment_text3.java

fragment_text3.java

package com.example.myapplication_one; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class fragment_text3 extends Fragment { @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_text_layout5,container,false); // View view2 = inflater.inflate(R.layout.activity_list_item,null); return view; } } fragment_text_layout5.xml

fragment_text_layout5.xml

效果:

最终实现效果


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3