Activity继承BaseActivity的使用(有相同不状态栏时很适用)

    xiaoxiao2021-03-25  98

    首先在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里其他控件的初始化 }
    转载请注明原文地址: https://ju.6miu.com/read-21136.html

    最新回复(0)