纯代码实现button+fragment底部菜单栏
来源:互联网 发布:社会图软件 编辑:程序博客网 时间:2024/06/07 16:17
首先说说为什么要用纯代码。
纯代码布局的适用性强,对于一些打包,混淆加密,资源管理等具备一定的优势。而且代码动态布局使用起来感觉让人更便利,一次封装就能随心所欲的控制。
但同时纯代码布局也会有很多弊端,比如界面无法用IDE及时实现可视化,调试困难,没有阅读纯代码习惯的话会造成一定的困扰,对新手来讲更是觉得有些难以阅读,如果项目架构不明确的话更是让人难以维护。
现在的纯代码布局多数用于一些SDK插件之中,打包的便利性会让SDK集成变得相当容易,抛弃了res文件的copy,开发也不用去利用Java有些跳跃的反射机制去读取资源ID,这样,集成起来可能只需要一个jar就可以了,然后配置主文件,调用接口即可。
好了,言归正传,开始说说纯代码实现的这个例子。
首先看下act的内容,相当简单。
package com.example.test;import java.util.ArrayList;import android.app.Activity;import android.os.Bundle;import com.example.test.fragment.AFragment;import com.example.test.fragment.BFragment;import com.example.test.fragment.CFragment;import com.example.test.fragment.DFragment;import com.example.test.frame.MainView;import com.example.test.frame.MenuInfo;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ArrayList<MenuInfo> infos = new ArrayList<MenuInfo>(); infos.add(new MenuInfo(0, R.drawable.b, R.drawable.a, "首页",new AFragment())); infos.add(new MenuInfo(1, R.drawable.b, R.drawable.a, "云播",new BFragment())); infos.add(new MenuInfo(2, R.drawable.b, R.drawable.a, "博客",new CFragment())); infos.add(new MenuInfo(3, R.drawable.b, R.drawable.a, "我",new DFragment())); MainView mv = new MainView(this,infos,0); setContentView(mv); } }
package com.example.test.frame;import android.app.Fragment;public class MenuInfo {public int id;public int downImg;public int upImg;public String text;public Fragment f;public MenuInfo(int id, int downImg, int upImg, String text,Fragment f) {super();this.id = id;this.downImg = downImg;this.upImg = upImg;this.text = text;this.f = f;}}
package com.example.test.frame;import java.util.ArrayList;import android.app.Activity;import android.app.FragmentTransaction;import android.view.Gravity;import android.view.View;import android.view.View.OnClickListener;import android.widget.FrameLayout;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;public class MainView extends LinearLayout implements OnClickListener {private Activity mActivity;private ArrayList<MenuInfo> infos;private ImageView iv;private TextView tv;private int oldId;private FrameLayout flayout;private int textUp = 0xffffffff;private int textDown = 0xff3e3e3e;public MainView(Activity act, ArrayList<MenuInfo> infos, int id) {super(act);this.mActivity = act;this.infos = infos;this.setOrientation(LinearLayout.VERTICAL);this.oldId = infos.get(0).id;init();initItem();defClick(id);}private void init() {flayout = new FrameLayout(mActivity);flayout.setId(0x9527);LinearLayout.LayoutParams llp = new LayoutParams(-1, -2);llp.weight = 1;this.addView(flayout, llp);}private void initItem() {LinearLayout bottomLayout = new LinearLayout(mActivity);bottomLayout.setOrientation(LinearLayout.HORIZONTAL);bottomLayout.setGravity(Gravity.CENTER);bottomLayout.setBackgroundColor(0xffeeeeee);this.addView(bottomLayout);LayoutParams lp = new LayoutParams(-1, -1);lp.weight = 1;LayoutParams llp = new LayoutParams(-2, -2);llp.gravity = Gravity.CENTER;for (MenuInfo mMenuInfo : infos) {LinearLayout item = new LinearLayout(mActivity);item.setOrientation(LinearLayout.VERTICAL);item.setId(mMenuInfo.id);item.setTag(mMenuInfo);item.setOnClickListener(this);bottomLayout.addView(item, lp);iv = new ImageView(mActivity);iv.setBackgroundResource(mMenuInfo.downImg);item.addView(iv, llp);tv = new TextView(mActivity);tv.setText(mMenuInfo.text);tv.setTextSize(13);tv.setTextColor(textDown);item.addView(tv, llp);}}private void setClickAt(int nowId) {downClick(oldId);upClick(nowId);this.oldId = nowId;}private void defClick(int id) {setClickAt(id);MenuInfo mMenuInfo = (MenuInfo) this.findViewById(id).getTag();mActivity.getFragmentManager().beginTransaction().add(0x9527, mMenuInfo.f, mMenuInfo.id + "").commit();}private void downClick(int id) {LinearLayout ll = (LinearLayout) this.findViewById(id);MenuInfo mMenuInfo = (MenuInfo) ll.getTag();ImageView iv = (ImageView) ll.getChildAt(0);iv.setBackgroundResource(mMenuInfo.downImg);TextView tv = (TextView) ll.getChildAt(1);tv.setTextColor(textUp);}private void upClick(int id) {LinearLayout ll = (LinearLayout) this.findViewById(id);MenuInfo mMenuInfo = (MenuInfo) ll.getTag();ImageView iv = (ImageView) ll.getChildAt(0);iv.setBackgroundResource(mMenuInfo.upImg);TextView tv = (TextView) ll.getChildAt(1);tv.setTextColor(textDown);}@Overridepublic void onClick(View v) {int id = v.getId();setClickAt(id);MenuInfo mMenuInfo = (MenuInfo) this.findViewById(id).getTag();FragmentTransaction transaction = mActivity.getFragmentManager().beginTransaction();transaction.replace(0x9527, mMenuInfo.f);//Toast.makeText(mActivity, mActivity.getFragmentManager().getBackStackEntryCount()+"", 0).show();//transaction.addToBackStack(null);transaction.commit();}}
好了,没错,看上去就这么多。省去了所有的配置文件,主要的代码就这样完成了。我们看下效果图。
0 0
- 纯代码实现button+fragment底部菜单栏
- Fragment实现底部菜单栏
- Fragment实现底部菜单栏
- Fragment实现底部菜单栏
- FragmentTabHost+Fragment实现底部菜单栏
- Fragment+FragmentTabHost实现底部菜单栏
- 使用Fragment实现底部菜单栏
- 使用Fragment实现底部菜单栏
- BottomNavigationView+fragment 实现底部菜单栏
- Fragment+FragmentTabHost实现底部菜单栏
- FragmentTabHost+Fragment实现底部菜单栏
- 底部菜单栏(二)使用Fragment实现底部菜单栏
- Android TabHost +Fragment 实现底部菜单栏
- Android TabHost +Fragment 实现底部菜单栏 .
- 【Android】底部菜单栏--Fragment+FragmentTabHost实现
- #Android笔记#fragment+fragmentTabHost实现底部菜单栏
- 【Android UI】RadioGroup+Fragment实现底部菜单栏
- Fragment+RadioGroup实现底部菜单栏(二)
- MovContext结构体
- ionic在app内部打开pdf文件
- 如何通过远程桌面重启计算机?
- 记录一篇演讲
- 决策树之cart算法
- 纯代码实现button+fragment底部菜单栏
- 有弹出页面的新增
- 简单谈谈关于产品的五个要点
- 设计模式:结构型模式
- 【FFT优化】[UOJ#34]多项式乘法
- The provisioning profile is expiring soon
- java json转对象
- 基于Android Studio的内存泄漏检测与解决全攻略
- 《中国哲学简史》读后感