Activity继承BaseActivity的使用(有相同不状态栏时很适用)
来源:互联网 发布:淘宝直通车添加关键词 编辑:程序博客网 时间:2024/06/03 21:29
首先在BaseActivity中写好其他Activity要继承的状态栏的布局:
布局文件activity_base.xml代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.chuangzhi.chuangzhi360.activity.BaseActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="50px" android:background="@drawable/top_bg"> <ImageView android:id="@+id/logo" android:layout_width="40px" android:layout_height="40px" android:layout_centerVertical="true" android:layout_marginLeft="20px" android:src="@drawable/yimei_logo"/> <TextView android:id="@+id/company_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@+id/logo" android:layout_marginLeft="20px" android:text="@string/ymys" android:textSize="16px" android:textColor="@color/white"/> <TextView android:id="@+id/date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:layout_marginRight="20px" android:textSize="16px" android:textColor="@color/white"/> <ImageView android:id="@+id/is_connect" android:layout_width="22px" android:layout_height="22px" android:src="@drawable/connect" android:layout_toLeftOf="@+id/date" android:layout_centerVertical="true" android:layout_marginRight="10px"/> </RelativeLayout> <!-- 为下个布局分配空间 --> <RelativeLayout android:id="@+id/all_content" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000"> </RelativeLayout></LinearLayout>
接下来BaseActivity.java的代码:
public class BaseActivity extends AppCompatActivity { TextView shopName; TextView date; /** * 判断当前界面的那个界面 */ ImageView isConnect; private RelativeLayout allContent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); hideStatusBar(); //影响状态栏 setContentView(R.layout.activity_base); } // 初始化 public void baseInit(){ date = (TextView) findViewById(R.id.date); isConnect = (ImageView) findViewById(R.id.is_connect); showDate(); showNetwork(); } // 设置要显示的布局方法 public void BaseSetContentView(int layoutID){ allContent = (RelativeLayout)findViewById(R.id.all_content); LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);// 把继承该BaseActivity的layoutID放进来 显示 View view = inflater.inflate(layoutID, null); allContent.addView(view); } private void hideStatusBar() { // TODO TODO TODO TODO Auto-generated method stub // 隐藏标题 requestWindowFeature(Window. FEATURE_NO_TITLE ); // 定义全屏参数 int flag = WindowManager.LayoutParams. FLAG_FULLSCREEN ; // 获得窗口对象 Window myWindow = this.getWindow(); // 设置 Flag 标识 myWindow.setFlags(flag,flag); } /** * 显示网络状态 */ private void showNetwork() { ConnectivityManager mConnectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); if (mNetworkInfo != null) { //网络可用 isConnect.setBackgroundResource(R.drawable.connect); }else { isConnect.setBackgroundResource(R.drawable.un_connect); } } private void showDate() { final Handler mHandler = new Handler(){ @Override public void handleMessage(Message msg) { if(msg.arg1==1){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm"); String dateStr = format.format(new Date()); date.setText(dateStr); } } }; //定义一个计时器,设置为延迟0ms后执行,每隔1min执行一次(这里如果设置为 timer.schedule(task,1000)表示执行一次) new Timer().schedule(new TimerTask(){ @Override public void run() { Message message = new Message(); message.arg1 = 1; mHandler.sendMessage(message); } }, 0, 1000); }}
接下来就是要继承BaseActivity的类,这里就举一个例好了:
public class MainActivity extends BaseActivity implements View.OnClickListener { public ImageView[] tabImages; public TextView[] tabTexts; public LinearLayout[] tabLayouts; public static Fragment currentFragment; public static FragmentManager manager; private RelativeLayout infoLayout; private ImageView contactBook; private ImageView setting; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);// 调用父类方法显示view BaseSetContentView(R.layout.activity_main); initView(); } private void initView() { baseInit(); //后面就是你的Activity里其他控件的初始化 }
1 0
- Activity继承BaseActivity的使用(有相同不状态栏时很适用)
- Activity继承BaseActivity的使用(使用相同布局)
- Activity继承BaseActivity的使用
- BaseActivity(Activity生命周期)
- 继承与BaseActivity的抽取
- activity使用theme.dialog且activity中有dialog时按返回按钮dialog消失activity不关闭的解决方法
- mysql使用(不适用)索引的情况
- mysql使用(不适用)索引
- BaseActivity 打开新的activity 新的activityForResult dip、px互换 toast打印 sp为字体大小单位时设置字体不随系统变化而变化封装类
- 打造一个属于自己的BaseActivity(沉浸状态栏,dialog,完全退出程序。应有尽有)
- Android 继承BaseActivity的典型用法
- 加载图片的时候使用classloader与不适用有什么区别吗??求各位指导下小白啊
- Android中BaseActivity的使用
- android开发不继承ActionBarActivity直接继承Activity的方法
- 当Activity在不继承FragmengActivity的情况下,Fragment中集成ViewpagerIndicator使用详解
- SPOJ 3267. D-query (主席树,查询区间有多少个不相同的数)
- Android基于4.4沉侵式状态栏和继承BaseActivity不用findViewbyid得到控件id
- Android基于4.4以上沉侵式状态栏和继承BaseActivity不用findViewbyid得到控件id
- 【Java集合的详细研究8】List,Set,Map用法以及区别
- Tensorflow lstm实现的小说撰写预测
- 一只大二狗的Android历程--国际化(作业)
- leetcode 210. Course Schedule II
- __ Uiautomator通过CTSHelper类查看报告
- Activity继承BaseActivity的使用(有相同不状态栏时很适用)
- css控制强制换行
- select2用法总结
- java工程师需要学习什么
- 服务器集群和反向代理
- 如何创建属于你自己的域名邮箱
- Java动态代理的两种实现方法
- Spring监听器
- 基于WebStorm, React和Ant.Design开发WebAppDemo